github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/cmd/idea/idea.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package idea
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path"
    23  	"text/template"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  const (
    29  	defaultGoSDK       = "Go"
    30  	imlTemplateContent = `<?xml version="1.0" encoding="UTF-8"?>
    31  <module type="GO_MODULE" version="4">
    32    <component name="NewModuleRootManager" inherit-compiler-output="true">
    33      <exclude-output />
    34      <content url="file://$MODULE_DIR$" />
    35      <orderEntry type="jdk" jdkName="{{.GoSDK}}" jdkType="Go SDK" />
    36      <orderEntry type="sourceFolder" forTests="false" />
    37    </component>
    38  </module>
    39  `
    40  	iprTemplateContent = `<?xml version="1.0" encoding="UTF-8"?>
    41  <project version="4">
    42    <component name="ProjectModuleManager">
    43      <modules>
    44        <module fileurl="file://$PROJECT_DIR$/{{.ProjectName}}.iml" filepath="$PROJECT_DIR$/{{.ProjectName}}.iml" />
    45      </modules>
    46    </component>
    47    <component name="ProjectRootManager" version="2" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="{{.GoSDK}}" project-jdk-type="Go SDK" />
    48    <component name="ProjectTasksOptions">
    49      <TaskOptions isEnabled="true">
    50        <option name="arguments" value="format runAll $FilePathRelativeToProjectRoot$" />
    51        <option name="checkSyntaxErrors" value="true" />
    52        <option name="description" value="" />
    53        <option name="exitCodeBehavior" value="ERROR" />
    54        <option name="fileExtension" value="go" />
    55        <option name="immediateSync" value="false" />
    56        <option name="name" value="godel" />
    57        <option name="output" value="" />
    58        <option name="outputFilters">
    59          <array />
    60        </option>
    61        <option name="outputFromStdout" value="false" />
    62        <option name="program" value="$ProjectFileDir$/godelw" />
    63        <option name="scopeName" value="Changed Files" />
    64        <option name="trackOnlyRoot" value="false" />
    65        <option name="workingDir" value="$ProjectFileDir$" />
    66        <envs />
    67      </TaskOptions>
    68    </component>
    69  </project>
    70  `
    71  )
    72  
    73  func CreateIdeaFiles(rootDir string) error {
    74  	projectName := path.Base(rootDir)
    75  
    76  	buffer := bytes.Buffer{}
    77  	templateValues := map[string]string{
    78  		"GoSDK":       defaultGoSDK,
    79  		"ProjectName": projectName,
    80  	}
    81  	imlTemplate := template.Must(template.New("iml").Parse(imlTemplateContent))
    82  	if err := imlTemplate.Execute(&buffer, templateValues); err != nil {
    83  		return errors.Wrapf(err, "Failed to execute template %s with values %v", imlTemplate, templateValues)
    84  	}
    85  
    86  	imlFilePath := path.Join(rootDir, projectName+".iml")
    87  	if err := ioutil.WriteFile(imlFilePath, buffer.Bytes(), 0644); err != nil {
    88  		return errors.Wrapf(err, "Failed to write .iml file to %s", imlFilePath)
    89  	}
    90  
    91  	iprTemplate := template.Must(template.New("modules").Parse(iprTemplateContent))
    92  	buffer = bytes.Buffer{}
    93  	if err := iprTemplate.Execute(&buffer, templateValues); err != nil {
    94  		return errors.Wrapf(err, "Failed to execute template %s with values %v", imlTemplate, templateValues)
    95  	}
    96  
    97  	iprFilePath := path.Join(rootDir, projectName+".ipr")
    98  	if err := ioutil.WriteFile(iprFilePath, buffer.Bytes(), 0644); err != nil {
    99  		return errors.Wrapf(err, "Failed to write .ipr file to %s", iprFilePath)
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func CleanIdeaFiles(rootDir string) error {
   106  	projectName := path.Base(rootDir)
   107  	for _, ext := range []string{"iml", "ipr", "iws"} {
   108  		currPath := path.Join(rootDir, fmt.Sprintf("%v.%v", projectName, ext))
   109  		if err := os.Remove(currPath); err != nil && !os.IsNotExist(err) {
   110  			return errors.Wrapf(err, "Failed to remove file %s", currPath)
   111  		}
   112  	}
   113  	return nil
   114  }