@@ -24,7 +24,6 @@ import (
2424 "io/fs"
2525 "os"
2626 "path/filepath"
27- "regexp"
2827 "slices"
2928 "strings"
3029)
@@ -33,6 +32,9 @@ type Config struct {
3332 // Filename to look for the root of a package.
3433 PackageFile []string `json:"package-file"`
3534
35+ // Setup filename, must be located in the same directory as the package file.
36+ SetupFile string `json:"setup-file"`
37+
3638 // Pattern to match filenames or directories.
3739 Match []string `json:"match"`
3840
@@ -43,8 +45,13 @@ type Config struct {
4345 ExcludePackages []string `json:"exclude-packages"`
4446}
4547
46- var multiLineCommentsRegex = regexp .MustCompile (`(?s)\s*/\*.*?\*/` )
47- var singleLineCommentsRegex = regexp .MustCompile (`\s*//.*\s*` )
48+ type Package struct {
49+ // Package directory path.
50+ Path string `json:"path"`
51+
52+ // Package setup configurations.
53+ Setup * map [string ]any `json:"setup"`
54+ }
4855
4956// Saves the config to the given file.
5057func (c * Config ) Save (file * os.File ) error {
@@ -61,18 +68,8 @@ func (c *Config) Save(file *os.File) error {
6168
6269// LoadConfig loads the config from the given path.
6370func LoadConfig (path string ) (* Config , error ) {
64- // Read the JSONC file.
65- sourceJsonc , err := os .ReadFile (path )
66- if err != nil {
67- return nil , err
68- }
69-
70- // Strip the comments and load the JSON.
71- sourceJson := multiLineCommentsRegex .ReplaceAll (sourceJsonc , []byte {})
72- sourceJson = singleLineCommentsRegex .ReplaceAll (sourceJson , []byte {})
73-
74- var config Config
75- err = json .Unmarshal (sourceJson , & config )
71+ // Read the config file.
72+ config , err := ReadJsonc [Config ](path )
7673 if err != nil {
7774 return nil , err
7875 }
@@ -85,7 +82,7 @@ func LoadConfig(path string) (*Config, error) {
8582 config .Match = []string {"*" }
8683 }
8784
88- return & config , nil
85+ return config , nil
8986}
9087
9188// Match returns true if the path matches any of the patterns.
@@ -156,12 +153,31 @@ func (c *Config) FindAllPackages(root string) ([]string, error) {
156153// Affected returns the packages that have been affected from diffs.
157154// If there are diffs on at leat one global file affecting all packages,
158155// then this returns all packages matched by the config.
159- func (c * Config ) Affected (log io.Writer , diffs []string ) ([]string , error ) {
156+ func (c * Config ) Affected (log io.Writer , diffs []string ) ([]Package , error ) {
160157 changed := c .Changed (log , diffs )
161158 if slices .Contains (changed , "." ) {
162- return c .FindAllPackages ("." )
159+ allPackages , err := c .FindAllPackages ("." )
160+ if err != nil {
161+ return nil , err
162+ }
163+ changed = allPackages
163164 }
164- return changed , nil
165+
166+ packages := make ([]Package , 0 , len (changed ))
167+ for _ , path := range changed {
168+ pkg := Package {Path : path }
169+ if c .SetupFile != "" {
170+ setup_file := filepath .Join (path , c .SetupFile )
171+ println (setup_file )
172+ setup , err := ReadJsonc [map [string ]any ](setup_file )
173+ if err != nil {
174+ return nil , err
175+ }
176+ pkg .Setup = setup
177+ }
178+ packages = append (packages , pkg )
179+ }
180+ return packages , nil
165181}
166182
167183// Changed returns the packages that have changed.
0 commit comments