Skip to content

Commit 0f2cc04

Browse files
committed
fix: cache files specific to target
1 parent b8fd559 commit 0f2cc04

File tree

6 files changed

+84
-13
lines changed

6 files changed

+84
-13
lines changed

.goreleaser.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ release:
7575
scoops:
7676
- name: spc
7777
repository:
78-
owner: damienbutt
79-
name: scoop-bucket
78+
owner: Norgate-AV
79+
name: scoop-bucket-crestron
8080
token: "{{ .Env.RELEASE_TOKEN }}"
8181
directory: bucket
8282
homepage: https://github.com/Norgate-AV/spc
83-
description: "CLI wrapper for the Crestron SIMPL+ Compiler"
83+
description: "A better way to compile Crestron SIMPL+ files"
8484
license: MIT

internal/cache/artifacts.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ func RestoreArtifacts(cacheDir, destDir string, outputs []string) error {
7272
// 1. The source file directory for .ush header files
7373
// 2. The SPlsWork directory for source-specific artifacts
7474
//
75+
// Only collects files for the specified target (e.g., if target="34", skips S2_* files)
7576
// Returns paths relative to the source directory (e.g., "example.ush", "SPlsWork/example.dll")
76-
func CollectOutputs(sourceFile string) ([]string, error) {
77+
func CollectOutputs(sourceFile string, target string) ([]string, error) {
7778
var outputs []string
7879

7980
// Extract base name without extension (e.g., "example1" from "example1.usp")
@@ -111,9 +112,9 @@ func CollectOutputs(sourceFile string) ([]string, error) {
111112
continue
112113
}
113114

114-
// Check if this file belongs to our source file
115-
// Match patterns: {basename}.* or S2_{basename}.*
116-
if isOutputFile(name, baseName) {
115+
// Check if this file belongs to our source file AND target
116+
// Match patterns: {basename}.* or S2_{basename}.* (depending on target)
117+
if isOutputFileForTarget(name, baseName, target) {
117118
// Store with SPlsWork/ prefix for proper path handling
118119
outputs = append(outputs, filepath.Join("SPlsWork", name))
119120
}
@@ -221,6 +222,51 @@ func isOutputFile(filename, baseName string) bool {
221222
return false
222223
}
223224

225+
// isOutputFileForTarget checks if a file belongs to the given source AND target
226+
// For target "34", only matches example1.* (not S2_example1.*)
227+
// For target "234", matches both example1.* and S2_example1.*
228+
func isOutputFileForTarget(filename, baseName, target string) bool {
229+
fileBase := filename[:len(filename)-len(filepath.Ext(filename))]
230+
231+
// Direct match: example1.dll, example1.cs, etc.
232+
// These are for Series 3 and 4
233+
if fileBase == baseName {
234+
return true
235+
}
236+
237+
// Target-prefixed match: S2_example1.c, S2_example1.h, S3_example1.*, S4_example1.*
238+
if len(fileBase) > 3 && fileBase[0] == 'S' && fileBase[2] == '_' {
239+
// Extract the series number
240+
seriesChar := fileBase[1]
241+
242+
// Extract the base name after prefix
243+
if fileBase[3:] == baseName {
244+
// Check if this series is in the target
245+
// For example, if target="34", we want Series 3 and 4, not Series 2
246+
switch seriesChar {
247+
case '2':
248+
return contains(target, '2')
249+
case '3':
250+
return contains(target, '3')
251+
case '4':
252+
return contains(target, '4')
253+
}
254+
}
255+
}
256+
257+
return false
258+
}
259+
260+
// contains checks if a string contains a specific character
261+
func contains(s string, ch byte) bool {
262+
for i := 0; i < len(s); i++ {
263+
if s[i] == ch {
264+
return true
265+
}
266+
}
267+
return false
268+
}
269+
224270
// copyFile copies a file from src to dst
225271
func copyFile(src, dst string) error {
226272
srcFile, err := os.Open(src)

internal/cache/cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func (c *Cache) Store(sourceFile string, cfg *config.Config, success bool) error
126126
}
127127

128128
// Collect outputs from both source dir and SPlsWork dir
129-
outputs, err := CollectOutputs(sourceFile)
129+
// Only collect files for the current target (prevents caching leftover files)
130+
outputs, err := CollectOutputs(sourceFile, cfg.Target)
130131
if err != nil {
131132
return fmt.Errorf("failed to collect outputs: %w", err)
132133
}

internal/cache/cache_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ func TestCollectOutputs_Filtering(t *testing.T) {
113113
err = os.WriteFile(ushFile, []byte("header"), 0o644)
114114
require.NoError(t, err)
115115

116-
// Collect outputs for example1.usp
117-
outputs, err := CollectOutputs(sourceFile)
116+
// Test 1: Collect outputs for target "234" (all series)
117+
outputs, err := CollectOutputs(sourceFile, "234")
118118
require.NoError(t, err)
119119

120120
// Should collect: 1 .ush file + 9 SPlsWork files = 10 total
121121
expectedCount := 10
122-
assert.Len(t, outputs, expectedCount, "Should collect .ush + SPlsWork files for example1.usp")
122+
assert.Len(t, outputs, expectedCount, "Should collect .ush + SPlsWork files for example1.usp with target 234")
123123

124124
// Verify correct files are included
125125
outputMap := make(map[string]bool)
@@ -141,6 +141,30 @@ func TestCollectOutputs_Filtering(t *testing.T) {
141141
assert.False(t, outputMap[filepath.Join("SPlsWork", "S2_example2.c")], "Should NOT include S2_example2.c")
142142
assert.False(t, outputMap[filepath.Join("SPlsWork", "Version.ini")], "Should NOT include shared library files")
143143
assert.False(t, outputMap[filepath.Join("SPlsWork", "ManagedUtilities.dll")], "Should NOT include shared library files")
144+
145+
// Test 2: Collect outputs for target "34" (no series 2)
146+
outputs34, err := CollectOutputs(sourceFile, "34")
147+
require.NoError(t, err)
148+
149+
// Should collect: 1 .ush file + 3 SPlsWork files (no S2_* files) = 4 total
150+
expectedCount34 := 4
151+
assert.Len(t, outputs34, expectedCount34, "Should collect only Series 3/4 files for target 34")
152+
153+
outputMap34 := make(map[string]bool)
154+
for _, output := range outputs34 {
155+
outputMap34[output] = true
156+
}
157+
158+
// Check that Series 3/4 files are included
159+
assert.True(t, outputMap34["example1.ush"], "Should include example1.ush")
160+
assert.True(t, outputMap34[filepath.Join("SPlsWork", "example1.dll")], "Should include example1.dll")
161+
assert.True(t, outputMap34[filepath.Join("SPlsWork", "example1.cs")], "Should include example1.cs")
162+
assert.True(t, outputMap34[filepath.Join("SPlsWork", "example1.inf")], "Should include example1.inf")
163+
164+
// Check that Series 2 files are NOT included
165+
assert.False(t, outputMap34[filepath.Join("SPlsWork", "S2_example1.c")], "Should NOT include S2_example1.c for target 34")
166+
assert.False(t, outputMap34[filepath.Join("SPlsWork", "S2_example1.h")], "Should NOT include S2_example1.h for target 34")
167+
assert.False(t, outputMap34[filepath.Join("SPlsWork", "S2_example1.elf")], "Should NOT include S2_example1.elf for target 34")
144168
}
145169

146170
func TestCache_StoreAndGet(t *testing.T) {

internal/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestLoad(t *testing.T) {
9999
abs, _ := filepath.Abs("C:/SPlusCC.exe")
100100
return abs
101101
}(),
102-
Target: "234",
102+
Target: "34",
103103
UserFolders: nil,
104104
},
105105
wantErr: false,

internal/config/loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestLoader_SetupViperDefaults(t *testing.T) {
2222
loader.setupViperDefaults()
2323

2424
assert.Equal(t, "C:/Program Files (x86)/Crestron/Simpl/SPlusCC.exe", viper.GetString("compiler_path"))
25-
assert.Equal(t, "234", viper.GetString("target"))
25+
assert.Equal(t, "34", viper.GetString("target"))
2626
assert.Equal(t, false, viper.GetBool("silent"))
2727
assert.Equal(t, false, viper.GetBool("verbose"))
2828
}

0 commit comments

Comments
 (0)