Saltar al contenido

~ / archivo / stackoverflow / 50180616

Command line tool to modify XML in .idea folder in JetBrains projects

StackOverflow · xml, intellij-idea, webstorm, jetbrains-ide · ayudé
  1. Alexander Mills pregunta +3 2018-05-04 17:53

    I use WebStorm and other JetBrains projects.

    I am looking for a command line tool that can modify the settings for a new project so that I don't have to do it manually.

    For example, I never want spellcheck to be active, and I believe this is the XML that controls spellcheck:

    <component name="InspectionProjectProfileManager">
      <profile version="1.0">
        <option name="myName" value="Project Default" />
        <inspection_tool class="SpellCheckingInspection" enabled="true" level="TYPO" enabled_by_default="false">
          <option name="processCode" value="true" />
          <option name="processLiterals" value="true" />
          <option name="processComments" value="true" />
        </inspection_tool>
      </profile>
    </component>
    

    Does anyone know of a JetBrains tool that can do this, or should I write something myself?

    The right way to do this will be for each user to modify the Default Settings of JetBrains products themselves.

    https://www.jetbrains.com/help/webstorm/accessing-default-settings.html

    However, I am still curious if there is a good way to use/create a command line to tool to change per project settings.

    • Meo 2018-05-04 19:06 how about configuring the default settings when you close all projects?
    • LazyOne 2018-05-04 21:13 See stackoverflow.com/a/14984416/783119
    • Alexander Mills 2018-05-09 20:41 @Meo is there a way to configure default settings w/o closing all projects?
    • wp78de 2018-05-13 23:42 Maybe a simple text replace? usring ruby, python, or whatever you have? re = /inspection_tool class="SpellCheckingInspection" enabled="true"/m subst = 'inspection_tool class="SpellCheckingInspection" enabled="false"' result = inputStr.gsub(re, subst)
  2. Miguel Ortiz yo respuesta +3 2018-05-15 20:52

    I found your question quite interesting. Here I've developed a management script example for your xml file.

    The script uses ElementTree a module which parses xml files and access it's objects (data) in a easy way to read or write changes to it.

    Also the script has a help and parameter options as examples which you can modify to get what you want.

    If you have linux or (cygwin|babun) in windows you could make a custom command (alias) in your bashrc to make it even easier and make it look alike to a CLI.

    Script

    #/usr/bin/python
    
    import sys
    import getopt
    import xml.etree.ElementTree as ET
    tree = ET.parse('properties.xml')
    root = tree.getroot()
    propFile='properties.xml'
    
    def procComments (newProp) :
        for prop in root.findall("./profile/inspection_tool/option/[@name='processComments']"):
            prop.set('value',newProp) 
        tree.write(propFile)
        print 'The new value of processComments is: '  + prop.get('value')
    
    
    def procLits (newProp) :
        for prop in root.findall("./profile/inspection_tool/option/[@name='processLiterals']"):
            prop.set('value',newProp) 
        tree.write(propFile)
        print 'The new value of processLiterals is: ' + prop.get('value')
    
    def usage() :
            print '-h display help'
            print 'Usage: python script.py <option> <value>'
            print 'Options: '
            print '     -c processComments property'
            print '     -l processLiterals property'
            print 'Values: may be true | false'
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'c:l:h')
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(2)
        elif opt in ('-c''--processComments'):
    
            userProp=sys.argv[2]
            procComments(userProp)
        elif opt in ('-l', '--processLiterals'):
            userProp=sys.argv[2]
            procLits(userProp)
        else:
            usage()
            sys.exit(2)
    

    Input file (properties.xml)

    <component name="InspectionProjectProfileManager">
      <profile version="1.0">
        <option name="myName" value="Project Default" />
        <inspection_tool class="SpellCheckingInspection" enabled="true" enabled_by_default="false" level="TYPO">
          <option name="processCode" value="true" />
          <option name="processLiterals" value="true" />
          <option name="processComments" value="true" />
        </inspection_tool>
      </profile>
    

    Example of usage:

    mortiz@alberta:~/.../cli_for_mills_xml$ python mills_parse_xml.py -c false
    The new value of processComments is: false
    

    Result:

    mortiz@alberta:~/Documents/projects/python/cli_for_mills_xml$ cat properties.xml
    <component name="InspectionProjectProfileManager">
      <profile version="1.0">
        <option name="myName" value="Project Default" />
        <inspection_tool class="SpellCheckingInspection" enabled="true" enabled_by_default="false" level="TYPO">
          <option name="processCode" value="true" />
          <option name="processLiterals" value="true" />
          <option name="processComments" value="false" />
        </inspection_tool>
      </profile>
    </component>
    
  3. Daniel Haley respuesta +3 2018-05-15 21:13

    If you know XPath, another option is to use xmlstarlet. Specifically, the ed (edit) command.

    Example command line...

    xmlstarlet ed -u "//inspection_tool[@class='SpellCheckingInspection']/@enabled" -v "false" some_settings_file.xml
    

    results...

    <component name="InspectionProjectProfileManager">
      <profile version="1.0">
        <option name="myName" value="Project Default"/>
        <inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
          <option name="processCode" value="true"/>
          <option name="processLiterals" value="true"/>
          <option name="processComments" value="true"/>
        </inspection_tool>
      </profile>
    </component>
    

    You can also edit the file in-place by adding -L (xmlstarlet ed -L ...).

  4. kriegaex respuesta +3 2018-05-16 12:40

    Does it really have to be a command-line tool? Why don't you just use IDEA's on-board means?

    Solution A: Ex-/import inspections directly

    Enter the inspections dialog in the settings. Then export, import, duplicate (with special name like "MyCompany default") all inspection settings to/from XML.

    [imagen]

    Solution B: Ex-/import project defaults

    Select "File" - "Other settings" - "Default settings", then deactivate the spelling inspection (similar to screenshot above) and save.

    Now just export all (or your own subset of) default settings into an importable JAR file via "File" - "Export settings". This has the advantage that you can bundle a package of default settings, e.g. code style, colors, toolbar buttons, file templates and many more. The minimum your need to export are "Default project" and "Inspection profiles (schemes)".

    [imagen]

← todos los hilos de StackOverflow