Skip to content

Commit 2f5820b

Browse files
committed
Merge branch 'main' into add-tests
2 parents 53a84e6 + 3607792 commit 2f5820b

File tree

14 files changed

+692
-119
lines changed

14 files changed

+692
-119
lines changed

.github/workflows/go.yml

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ permissions:
44
contents: write
55

66
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v4
12+
- name: Set up Go
13+
uses: actions/setup-go@v4
14+
- name: Build CLI for Linux
15+
run: |
16+
GOOS=linux GOARCH=amd64 go build -o cli-v2-linux ./cli-v2.go
17+
- name: Build CLI for Windows
18+
run: |
19+
GOOS=windows GOARCH=amd64 go build -o cli-v2.exe ./cli-v2.go
20+
- name: Build CLI for macOS
21+
run: |
22+
GOOS=darwin GOARCH=amd64 go build -o cli-v2-macos ./cli-v2.go
23+
- name: Upload CLI binaries
24+
uses: actions/upload-artifact@v4
25+
with:
26+
name: cli-binaries
27+
path: |
28+
cli-v2-linux
29+
cli-v2.exe
30+
cli-v2-macos
31+
732
test:
833
runs-on: ubuntu-latest
934
steps:
@@ -24,8 +49,53 @@ jobs:
2449
run: |
2550
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --force-coverage-parser go -r unit.coverage.out
2651
52+
ittest:
53+
needs: build
54+
runs-on: ${{ matrix.os }}
55+
strategy:
56+
matrix:
57+
os: [ubuntu-latest, windows-latest, macos-latest]
58+
steps:
59+
- name: Checkout
60+
uses: actions/checkout@v4
61+
- name: Download CLI binaries
62+
uses: actions/download-artifact@v4
63+
with:
64+
name: cli-binaries
65+
path: .
66+
- name: Select correct binary
67+
shell: bash
68+
run: |
69+
if [ "${{ matrix.os }}" = "windows-latest" ]; then
70+
# Keep the .exe extension for Windows
71+
echo "Using Windows binary with .exe extension"
72+
elif [ "${{ matrix.os }}" = "macos-latest" ]; then
73+
mv cli-v2-macos cli-v2
74+
else
75+
mv cli-v2-linux cli-v2
76+
fi
77+
- name: Make binary executable
78+
if: matrix.os != 'windows-latest'
79+
run: chmod +x cli-v2
80+
- name: Install dependencies from .codacy/codacy.yaml
81+
if: matrix.os != 'windows-latest'
82+
run: |
83+
./cli-v2 install
84+
# Disable windows it test for now.
85+
# - name: Install dependencies from .codacy/codacy.yaml (Windows)
86+
# if: matrix.os == 'windows-latest'
87+
# shell: pwsh
88+
# run: |
89+
# Get-ChildItem
90+
# Write-Host "Current directory contents:"
91+
# dir
92+
# Write-Host "Node.js version:"
93+
# node --version
94+
# Write-Host "Attempting to run CLI..."
95+
# .\cli-v2.exe install
96+
2797
release:
28-
needs: test
98+
needs: [test, ittest]
2999
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event_name == 'push'
30100
runs-on: ubuntu-latest
31101
steps:

cmd/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ var analyzeCmd = &cobra.Command{
207207
eslint := config.Config.Tools()["eslint"]
208208
eslintInstallationDirectory := eslint.Info()["installDir"]
209209
nodeRuntime := config.Config.Runtimes()["node"]
210-
nodeBinary := nodeRuntime.Info()["node"]
210+
nodeBinary := nodeRuntime.Binaries["node"]
211211

212212
log.Printf("Running %s...\n", toolToAnalyze)
213213
if outputFormat == "sarif" {

cmd/install.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,19 @@ var installCmd = &cobra.Command{
2020
Short: "Installs the tools specified in the project's config-file.",
2121
Long: "Installs all runtimes and tools specified in the project's config-file file.",
2222
Run: func(cmd *cobra.Command, args []string) {
23-
// install runtimes
24-
fetchRuntimes(&cfg.Config)
25-
// install tools
26-
fetchTools(&cfg.Config)
23+
installRuntimes(&cfg.Config)
24+
installTools(&cfg.Config)
2725
},
2826
}
2927

30-
func fetchRuntimes(config *cfg.ConfigType) {
31-
for _, r := range config.Runtimes() {
32-
switch r.Name() {
33-
case "node":
34-
err := cfg.InstallNode(r)
35-
if err != nil {
36-
log.Fatal(err)
37-
}
38-
default:
39-
log.Fatal("Unknown runtime:", r.Name())
40-
}
28+
func installRuntimes(config *cfg.ConfigType) {
29+
err := cfg.InstallRuntimes()
30+
if err != nil {
31+
log.Fatal(err)
4132
}
4233
}
4334

44-
func fetchTools(config *cfg.ConfigType) {
35+
func installTools(config *cfg.ConfigType) {
4536
for _, tool := range config.Tools() {
4637
switch tool.Name() {
4738
case "eslint":

config-file/configFile.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config_file
22

33
import (
44
"codacy/cli-v2/config"
5+
"codacy/cli-v2/plugins"
56
"gopkg.in/yaml.v3"
67
"os"
78
)
@@ -17,12 +18,22 @@ func parseConfigFile(configContents []byte) error {
1718
return err
1819
}
1920

21+
// Convert the runtime strings to RuntimeConfig objects
22+
runtimeConfigs := make([]plugins.RuntimeConfig, 0, len(configFile.RUNTIMES))
2023
for _, rt := range configFile.RUNTIMES {
2124
ct, err := parseConfigTool(rt)
2225
if err != nil {
2326
return err
2427
}
25-
config.Config.AddRuntime(config.NewRuntime(ct.name, ct.version))
28+
runtimeConfigs = append(runtimeConfigs, plugins.RuntimeConfig{
29+
Name: ct.name,
30+
Version: ct.version,
31+
})
32+
}
33+
34+
// Add all runtimes at once
35+
if err := config.Config.AddRuntimes(runtimeConfigs); err != nil {
36+
return err
2637
}
2738

2839
for _, tl := range configFile.TOOLS {

config/config.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"log"
55
"os"
66
"path/filepath"
7+
8+
"codacy/cli-v2/plugins"
79
)
810

911
type ConfigType struct {
@@ -14,7 +16,7 @@ type ConfigType struct {
1416
localCodacyDirectory string
1517
projectConfigFile string
1618

17-
runtimes map[string]*Runtime
19+
runtimes map[string]*plugins.RuntimeInfo
1820
tools map[string]*Runtime
1921
}
2022

@@ -42,12 +44,23 @@ func (c *ConfigType) ProjectConfigFile() string {
4244
return c.projectConfigFile
4345
}
4446

45-
func (c *ConfigType) Runtimes() map[string]*Runtime {
47+
func (c *ConfigType) Runtimes() map[string]*plugins.RuntimeInfo {
4648
return c.runtimes
4749
}
4850

49-
func (c *ConfigType) AddRuntime(r *Runtime) {
50-
c.runtimes[r.Name()] = r
51+
func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {
52+
// Process the runtime configurations using the plugins.ProcessRuntimes function
53+
runtimeInfoMap, err := plugins.ProcessRuntimes(configs, c.runtimesDirectory)
54+
if err != nil {
55+
return err
56+
}
57+
58+
// Store the runtime information in the config
59+
for name, info := range runtimeInfoMap {
60+
c.runtimes[name] = info
61+
}
62+
63+
return nil
5164
}
5265

5366
// TODO do inheritance with tool
@@ -103,7 +116,7 @@ func Init() {
103116

104117
Config.initCodacyDirs()
105118

106-
Config.runtimes = make(map[string]*Runtime)
119+
Config.runtimes = make(map[string]*plugins.RuntimeInfo)
107120
Config.tools = make(map[string]*Runtime)
108121
}
109122

config/eslint-utils.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"os/exec"
77
"path"
8+
"codacy/cli-v2/plugins"
89
)
910

1011
func genInfoEslint(r *Runtime) map[string]string {
@@ -20,20 +21,20 @@ func genInfoEslint(r *Runtime) map[string]string {
2021
/*
2122
* This installs eslint using node's npm alongside its sarif extension
2223
*/
23-
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime, registry string) error {
24+
func InstallEslint(nodeRuntime *plugins.RuntimeInfo, eslint *Runtime, registry string) error {
2425
log.Println("Installing ESLint")
2526

2627
eslintInstallArg := fmt.Sprintf("%s@%s", eslint.Name(), eslint.Version())
2728
if registry != "" {
2829
fmt.Println("Using registry:", registry)
29-
configCmd := exec.Command(nodeRuntime.Info()["npm"], "config", "set", "registry", registry)
30+
configCmd := exec.Command(nodeRuntime.Binaries["npm"], "config", "set", "registry", registry)
3031
if configOut, err := configCmd.Output(); err != nil {
3132
fmt.Println("Error setting npm registry:", err)
3233
fmt.Println(string(configOut))
3334
return err
3435
}
3536
}
36-
cmd := exec.Command(nodeRuntime.Info()["npm"], "install", "--prefix", eslint.Info()["installDir"],
37+
cmd := exec.Command(nodeRuntime.Binaries["npm"], "install", "--prefix", eslint.Info()["installDir"],
3738
eslintInstallArg, "@microsoft/eslint-formatter-sarif")
3839
fmt.Println(cmd.String())
3940
// to use the chdir command we needed to create the folder before, we can change this after

config/node-utils.go

Lines changed: 0 additions & 90 deletions
This file was deleted.

config/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
)
66

7+
// Note that this is only used by tools
78
type Runtime struct {
89
name string
910
version string
@@ -26,10 +27,9 @@ func (r *Runtime) Info() map[string]string {
2627
return r.info
2728
}
2829

30+
// populateInfo populates the runtime info
2931
func (r *Runtime) populateInfo() {
3032
switch r.Name() {
31-
case "node":
32-
r.info = genInfoNode(r)
3333
case "eslint":
3434
r.info = genInfoEslint(r)
3535
}

0 commit comments

Comments
 (0)