Exemplo n.º 1
0
 def run(self, type="RUN"):
     self.type = type
     self.file_name = getFileName()
     self.project = Project(self.file_name)
     if self.project.is_go():
         self.executeProject()
     elif isGoFile(self.file_name):
         self.executeFile()
     else:
         sublime.error_message("Cannot %s a non-Go file" % type.lower())
Exemplo n.º 2
0
class GoBuildCommand(sublime_plugin.WindowCommand):

    def run(self, type="RUN"):
        self.type = type
        self.file_name = getFileName()
        self.project = Project(self.file_name)
        if self.project.is_go():
            self.executeProject()
        elif isGoFile(self.file_name):
            self.executeFile()
        else:
            sublime.error_message("Cannot %s a non-Go file" % type.lower())

    def setEnv(self):
        append = ""
        self.GOPATH = ""

        if "GOPATH" in os.environ:
            self.GOPATH = os.environ['GOPATH']
            append = self.GOPATH + ";"

        os.environ['GOPATH'] = append + self.project.file_path
        os.putenv("GOPATH", append + self.project.file_path)

    def restoreEnv(self):
        if self.GOPATH != "":
            os.environ['GOPATH'] = self.GOPATH
            os.putenv("GOPATH", self.GOPATH)

        self.GOPATH = ""

    def executeFile(self):
        gobin = get_go_bin_path()
        if self.type == "RUN":
            command = [gobin, "run", self.file_name]
        elif self.type == "BUILD":
            command = [gobin, "build", "-x", "-v", self.file_name]
        elif self.type == "TEST":
            command = [gobin, "test", self.file_name]
        else:
            sublime.error_message("Unknown command: " + self.type)
            self.restoreEnv()
            return

        getView().window().run_command("exec", {'kill': True})
        getView().window().run_command("exec", {
            'shell': True,
            'cmd': command,
            'working_dir': os.path.dirname(self.file_name),
            'file_regex': '^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)',
        })

    def run_command(self, command):
        file_regex = '^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)'
        getView().window().run_command("exec", {'kill': True})
        getView().window().run_command(
            "exec", {'shell': True, 'cmd': [command],
                     'working_dir': self.project.base_path,
                     'file_regex': file_regex})

    def executeProject(self):
        project = self.project
        sublime.status_message("Building project: " + project.name)

        self.setEnv()
        try:
            gobin = get_go_bin_path(self.project.base_path)

            if self.type == "INSTALL":
                if project.target is None:
                    sublime.error_message("Can't infer project target "
                                          "package.")
                    return
                command = "%s install %s" % (gobin, project.target)
                self.run_command(command)

            elif self.type == "TEST":
                test_pkgs = project.settings.get("test_pkgs", [])
                if not test_pkgs:
                    sublime.error_message("No test_pkgs specified in project.")
                    return
                command = "%s test %s" % (gobin, ' '.join(test_pkgs))
                self.run_command(command)

            elif self.type == "RUN":
                if project.main is None:
                    sublime.error_message("Can't infer project main file.")
                    return
                command = "%s run %s" % (gobin, project.main)
                self.window.show_input_panel("Run command: ", command,
                                             self.run_command, None, None)

            else:
                sublime.error_message("Unknown command: " + self.type)
                return

        finally:
            self.restoreEnv()