github.com/sercand/please@v13.4.0+incompatible/src/ide/intellij/project.go (about)

     1  package intellij
     2  
     3  import (
     4  	"github.com/thought-machine/please/src/core"
     5  	"encoding/xml"
     6  	"fmt"
     7  	"io"
     8  )
     9  
    10  // Misc is an ancillary structure that handles things like Java source level.
    11  type Misc struct {
    12  	XMLName   xml.Name      `xml:"project"`
    13  	Version   int           `xml:"version,attr"`
    14  	Component MiscComponent `xml:"component"`
    15  }
    16  
    17  func newMisc(javaSourceLevel int) Misc {
    18  	return Misc{
    19  		Version:   4,
    20  		Component: newMiscComponent(javaSourceLevel),
    21  	}
    22  }
    23  
    24  func (misc *Misc) toXML(w io.Writer) {
    25  	w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))
    26  	content, err := xml.MarshalIndent(misc, "", "   ")
    27  
    28  	if err  == nil {
    29  		w.Write(content)
    30  	}
    31  }
    32  
    33  // MiscComponent is the main Misc wrapper type.
    34  type MiscComponent struct {
    35  	XMLName        xml.Name   `xml:"component"`
    36  	Name           string     `xml:"name,attr"`
    37  	Version        int        `xml:"version,attr"`
    38  	LanguageLevel  string     `xml:"languageLevel,attr"`
    39  	Default        bool       `xml:"default,attr"`
    40  	ProjectJdkName string     `xml:"project-jdk-name,attr"`
    41  	ProjectJdkType string     `xml:"project-jdk-type,attr"`
    42  	Output         MiscOutput `xml:"output"`
    43  }
    44  
    45  func newMiscComponent(javaSourceLevel int) MiscComponent {
    46  	format := "JDK_%d"
    47  	if javaSourceLevel < 10 {
    48  		format = "JDK_1_%d"
    49  	}
    50  	return MiscComponent{
    51  		Name:           "ProjectRootManager",
    52  		Version:        2,
    53  		Default:        false,
    54  		LanguageLevel:  fmt.Sprintf(format, javaSourceLevel),
    55  		ProjectJdkName: fmt.Sprintf("%d", javaSourceLevel),
    56  		ProjectJdkType: "JavaSDK",
    57  		Output:         newMiscOutput(),
    58  	}
    59  }
    60  
    61  // MiscOutput determines where intellij puts the code it has compiled itself.
    62  type MiscOutput struct {
    63  	XMLName xml.Name `xml:"output"`
    64  	URL     string   `xml:"url,attr"`
    65  }
    66  
    67  func newMiscOutput() MiscOutput {
    68  	return MiscOutput{
    69  		URL: "file://$PROJECT_DIR$/out",
    70  	}
    71  }
    72  
    73  // Modules are the main structure that tells IntelliJ where to find all the modules it knows about.
    74  type Modules struct {
    75  	XMLName xml.Name `xml:"project"`
    76  	Version int `xml:"version,attr"`
    77  	Component ModulesComponent `xml:"component"`
    78  }
    79  
    80  func newModules(targets core.BuildTargets) Modules {
    81  	return Modules{
    82  		Version:   4,
    83  		Component: newModulesComponent(targets),
    84  	}
    85  }
    86  
    87  func (modules *Modules) toXML(w io.Writer) {
    88  	w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))
    89  	content, err := xml.MarshalIndent(modules, "", "   ")
    90  
    91  	if err  == nil {
    92  		w.Write(content)
    93  	}
    94  }
    95  
    96  // ModulesComponent represents all modules in the workspace.
    97  type ModulesComponent struct {
    98  	XMLName xml.Name  `xml:"component"`
    99  	Name string `xml:"name,attr"`
   100  	Modules []ModulesModule `xml:"modules>module"`
   101  }
   102  
   103  func newModulesComponent(targets core.BuildTargets) ModulesComponent {
   104  	component := ModulesComponent{
   105  		Name: "ProjectModuleManager",
   106  	}
   107  
   108  	for _, t := range targets {
   109  		component.Modules = append(component.Modules, newModulesModule(t.Label))
   110  	}
   111  
   112  	return component
   113  }
   114  
   115  // ModulesModule represents one module in the workspace, and where to find its definition.
   116  type ModulesModule struct {
   117  	XMLName  xml.Name `xml:"module"`
   118  	FileURL  string   `xml:"fileurl,attr"`
   119  	FilePath string   `xml:"filepath,attr"`
   120  }
   121  
   122  func newModulesModule(label core.BuildLabel) ModulesModule {
   123  	filePath := "$PROJECT_DIR$/" + label.PackageDir() + "/" + fmt.Sprintf("%s.iml", moduleName(label))
   124  	return ModulesModule{
   125  		FileURL:  "file://"+filePath,
   126  		FilePath: filePath,
   127  	}
   128  }