Skip to content

Commit

Permalink
Merge 98585fc into 9739a5f
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoCostaCodacy authored May 22, 2024
2 parents 9739a5f + 98585fc commit 0e21052
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 194 deletions.
9 changes: 9 additions & 0 deletions Formula/codacy-cli-v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CodacyCliV2 < Formula
version "0.1.0-main.35.70f0f48"
url "https://raw.githubusercontent.com/codacy/codacy-cli-v2/0.1.0-main.35.70f0f48/codacy-cli.sh"
sha256 "fb616e2f5c639985566c81a6e6ce51db2e8de56bf217e837d13efe2f3ccc3042"

def install
bin.install "codacy-cli.sh" => "codacy-cli"
end
end
8 changes: 4 additions & 4 deletions cli-v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package main

import (
"codacy/cli-v2/cmd"
cfg "codacy/cli-v2/config"
"codacy/cli-v2/config"
cfg "codacy/cli-v2/config-file"
"log"
)

func main() {
cfg.Init()
config.Init()

runtimes, configErr := cfg.ReadConfigFile(cfg.Config.ProjectConfigFile())
configErr := cfg.ReadConfigFile(config.Config.ProjectConfigFile())
if configErr != nil {
log.Fatal(configErr)
}
cfg.Config.SetRuntimes(runtimes)

cmd.Execute()
}
19 changes: 7 additions & 12 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"codacy/cli-v2/config"
"codacy/cli-v2/tools"
"fmt"
"log"
"os"

Expand All @@ -22,27 +21,23 @@ var analyzeCmd = &cobra.Command{
Short: "Runs all linters.",
Long: "Runs all tools for all runtimes.",
Run: func(cmd *cobra.Command, args []string) {

eslintRunInfo, err := config.GetToolRunInfo("eslint")
if err != nil {
log.Fatal(err)
}

workDirectory, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

eslintInstallationDirectory := eslintRunInfo["eslintInstallationDirectory"]
nodeBinary := eslintRunInfo["nodeBinary"]

if len(args) == 0 {
log.Fatal("You need to specify the tool you want to run for now! ;D")
}

fmt.Printf("Running %s...\n", args[0])
eslint := config.Config.Tools()["eslint"]
eslintInstallationDirectory := eslint.Info()["installDir"]
nodeRuntime := config.Config.Runtimes()["node"]
nodeBinary := nodeRuntime.Info()["node"]

log.Printf("Running %s...\n", args[0])
if outputFile != "" {
fmt.Printf("Output will be available at %s\n", outputFile)
log.Printf("Output will be available at %s\n", outputFile)
}

tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
Expand Down
48 changes: 15 additions & 33 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package cmd

import (
cfg "codacy/cli-v2/config"
toolutils "codacy/cli-v2/tool-utils"
"codacy/cli-v2/utils"
"github.com/spf13/cobra"
"log"
"os"
"path/filepath"
)

func init() {
Expand All @@ -16,38 +12,21 @@ func init() {

var installCmd = &cobra.Command{
Use: "install",
Short: "Installs the tools specified in the project's config.",
Long: "Installs all runtimes and tools specified in the project's config file.",
Short: "Installs the tools specified in the project's config-file.",
Long: "Installs all runtimes and tools specified in the project's config-file file.",
Run: func(cmd *cobra.Command, args []string) {
// install runtimes
fetchRuntimes(cfg.Config.Runtimes(), cfg.Config.RuntimesDirectory())
fetchRuntimes(&cfg.Config)
// install tools
for _, r := range cfg.Config.Runtimes() {
fetchTools(r, cfg.Config.RuntimesDirectory(), cfg.Config.ToolsDirectory())
}
fetchTools(&cfg.Config)
},
}

func fetchRuntimes(runtimes map[string]*cfg.Runtime, runtimesDirectory string) {
for _, r := range runtimes {
func fetchRuntimes(config *cfg.ConfigType) {
for _, r := range config.Runtimes() {
switch r.Name() {
case "node":
// TODO should delete downloaded archive
// TODO check for deflated archive
log.Println("Fetching node...")
downloadNodeURL := cfg.GetNodeDownloadURL(r)
nodeTar, err := utils.DownloadFile(downloadNodeURL, runtimesDirectory)
if err != nil {
log.Fatal(err)
}

// deflate node archive
t, err := os.Open(nodeTar)
defer t.Close()
if err != nil {
log.Fatal(err)
}
err = utils.ExtractTarGz(t, runtimesDirectory)
err := cfg.InstallNode(r)
if err != nil {
log.Fatal(err)
}
Expand All @@ -57,13 +36,16 @@ func fetchRuntimes(runtimes map[string]*cfg.Runtime, runtimesDirectory string) {
}
}

func fetchTools(runtime *cfg.Runtime, runtimesDirectory string, toolsDirectory string) {
for _, tool := range runtime.Tools() {
func fetchTools(config *cfg.ConfigType) {
for _, tool := range config.Tools() {
switch tool.Name() {
case "eslint":
npmPath := filepath.Join(runtimesDirectory, cfg.GetNodeFileName(runtime),
"bin", "npm")
toolutils.InstallESLint(npmPath, "eslint@" + tool.Version(), toolsDirectory)
// eslint needs node runtime
nodeRuntime := config.Runtimes()["node"]
err := cfg.InstallEslint(nodeRuntime, tool)
if err != nil {
log.Fatal(err)
}
default:
log.Fatal("Unknown tool:", tool.Name())
}
Expand Down
28 changes: 11 additions & 17 deletions config/configFile.go → config-file/configFile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config
package config_file

import (
"codacy/cli-v2/config"
"gopkg.in/yaml.v3"
"os"
)
Expand All @@ -10,42 +11,35 @@ type configFile struct {
TOOLS []string
}

func parseConfigFile(configContents []byte) (map[string]*Runtime, error) {
func parseConfigFile(configContents []byte) error {
configFile := configFile{}
if err := yaml.Unmarshal(configContents, &configFile); err != nil {
return nil, err
return err
}

runtimes := make(map[string]*Runtime)
for _, rt := range configFile.RUNTIMES {
ct, err := parseConfigTool(rt)
if err != nil {
return nil, err
}
runtimes[ct.name] = &Runtime{
name: ct.name,
version: ct.version,
return err
}
config.Config.AddRuntime(config.NewRuntime(ct.name, ct.version))
}

for _, tl := range configFile.TOOLS {
ct, err := parseConfigTool(tl)
if err != nil {
return nil, err
}
switch ct.name {
case "eslint":
runtimes["node"].AddTool(ct)
return err
}
config.Config.AddTool(config.NewRuntime(ct.name, ct.version))
}

return runtimes, nil
return nil
}

func ReadConfigFile(configPath string) (map[string]*Runtime, error) {
func ReadConfigFile(configPath string) error {
content, err := os.ReadFile(configPath)
if err != nil {
return nil, err
return err
}

return parseConfigFile(content)
Expand Down
2 changes: 1 addition & 1 deletion config/configTool.go → config-file/configTool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package config_file

import (
"errors"
Expand Down
38 changes: 26 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
)

type configType struct {
type ConfigType struct {
homePath string
codacyDirectory string
runtimesDirectory string
Expand All @@ -15,41 +15,51 @@ type configType struct {
projectConfigFile string

runtimes map[string]*Runtime
tools map[string]*Runtime
}

func (c *configType) HomePath() string {
func (c *ConfigType) HomePath() string {
return c.homePath
}

func (c *configType) CodacyDirectory() string {
func (c *ConfigType) CodacyDirectory() string {
return c.codacyDirectory
}

func (c *configType) RuntimesDirectory() string {
func (c *ConfigType) RuntimesDirectory() string {
return c.runtimesDirectory
}

func (c *configType) ToolsDirectory() string {
func (c *ConfigType) ToolsDirectory() string {
return c.toolsDirectory
}

func (c *configType) LocalCodacyDirectory() string {
func (c *ConfigType) LocalCodacyDirectory() string {
return c.localCodacyDirectory
}

func (c *configType) ProjectConfigFile() string {
func (c *ConfigType) ProjectConfigFile() string {
return c.projectConfigFile
}

func (c *configType) Runtimes() map[string]*Runtime {
func (c *ConfigType) Runtimes() map[string]*Runtime {
return c.runtimes
}

func (c *configType) SetRuntimes(runtimes map[string]*Runtime) {
c.runtimes = runtimes
func (c *ConfigType) AddRuntime(r *Runtime) {
c.runtimes[r.Name()] = r
}

func (c *configType) initCodacyDirs() {
// TODO do inheritance with tool
func (c *ConfigType) Tools() map[string]*Runtime {
return c.tools
}

func (c *ConfigType) AddTool(t *Runtime) {
c.tools[t.Name()] = t
}

func (c *ConfigType) initCodacyDirs() {
c.codacyDirectory = filepath.Join(c.homePath, ".cache", "codacy")
err := os.MkdirAll(c.codacyDirectory, 0777)
if err != nil {
Expand Down Expand Up @@ -84,6 +94,10 @@ func Init() {
Config.homePath = homePath

Config.initCodacyDirs()

Config.runtimes = make(map[string]*Runtime)
Config.tools = make(map[string]*Runtime)
}

var Config = configType{}
// Global singleton config-file
var Config = ConfigType{}
35 changes: 35 additions & 0 deletions config/eslint-utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"fmt"
"log"
"os/exec"
"path"
)

func genInfoEslint(r *Runtime) map[string]string {
eslintFolder := fmt.Sprintf("%s@%s", r.Name(), r.Version())
installDir := path.Join(Config.ToolsDirectory(), eslintFolder)

return map[string]string{
"installDir": installDir,
"eslint": path.Join(installDir, "node_modules", ".bin", "eslint"),
}
}

/*
* This installs eslint using node's npm alongside its sarif extension
*/
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime) error {
log.Println("Installing ESLint")

eslintInstallArg := fmt.Sprintf("%s@%s", eslint.Name(), eslint.Version())
cmd := exec.Command(nodeRuntime.Info()["npm"], "install", "--prefix", eslint.Info()["installDir"],
eslintInstallArg, "@microsoft/eslint-formatter-sarif")
// to use the chdir command we needed to create the folder before, we can change this after
// cmd.Dir = eslintInstallationFolder
stdout, err := cmd.Output()
// Print the output
fmt.Println(string(stdout))
return err
}
Loading

0 comments on commit 0e21052

Please sign in to comment.