Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ lint:
xunitReport: ""
jsonFile: ""
ignoreNoqa: false
noCache: false
skip: {}
cache:
directory: .mendix-cache/mxlint
enable: true
modelsource: modelsource
projectDirectory: .
export:
Expand Down
23 changes: 23 additions & 0 deletions lint/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,29 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
)

const cacheVersion = "v1"

var cacheDirConfig = struct {
mu sync.RWMutex
dir string
}{}

func SetCacheDirectory(path string) {
cacheDirConfig.mu.Lock()
defer cacheDirConfig.mu.Unlock()
cacheDirConfig.dir = strings.TrimSpace(path)
}

func getConfiguredCacheDirectory() string {
cacheDirConfig.mu.RLock()
defer cacheDirConfig.mu.RUnlock()
return cacheDirConfig.dir
}

// CacheKey represents the unique identifier for a cached result
type CacheKey struct {
RuleHash string `json:"rule_hash"`
Expand All @@ -25,6 +44,10 @@ type CachedTestcase struct {

// getCacheDir returns the cache directory path
func getCacheDir() (string, error) {
if configured := getConfiguredCacheDirectory(); configured != "" {
return configured, nil
}

homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
Expand Down
14 changes: 11 additions & 3 deletions lint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const configFileName = "mxlint.yaml"
type Config struct {
Rules ConfigRulesSpec `yaml:"rules"`
Lint ConfigLintSpec `yaml:"lint"`
Cache ConfigCacheSpec `yaml:"cache"`
Export ConfigExportSpec `yaml:"export"`
Serve ConfigServeSpec `yaml:"serve"`
Modelsource string `yaml:"modelsource"`
Expand All @@ -37,10 +38,14 @@ type ConfigLintSpec struct {
XunitReport string `yaml:"xunitReport"`
JSONFile string `yaml:"jsonFile"`
IgnoreNoqa *bool `yaml:"ignoreNoqa"`
NoCache *bool `yaml:"noCache"`
Skip map[string][]ConfigSkipRule `yaml:"skip"`
}

type ConfigCacheSpec struct {
Directory string `yaml:"directory"`
Enable *bool `yaml:"enable"`
}

type ConfigServeSpec struct {
Port *int `yaml:"port"`
Debounce *int `yaml:"debounce"`
Expand Down Expand Up @@ -288,8 +293,11 @@ func mergeConfig(base *Config, overlay *Config) {
if overlay.Lint.IgnoreNoqa != nil {
base.Lint.IgnoreNoqa = overlay.Lint.IgnoreNoqa
}
if overlay.Lint.NoCache != nil {
base.Lint.NoCache = overlay.Lint.NoCache
if strings.TrimSpace(overlay.Cache.Directory) != "" {
base.Cache.Directory = strings.TrimSpace(overlay.Cache.Directory)
}
if overlay.Cache.Enable != nil {
base.Cache.Enable = overlay.Cache.Enable
}

if overlay.Serve.Port != nil {
Expand Down
52 changes: 50 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"text/tabwriter"

"github.com/mxlint/mxlint-cli/lint"
Expand Down Expand Up @@ -48,6 +49,8 @@ func main() {
log.SetLevel(logrus.InfoLevel)
}
mpr.SetLogger(log)
lint.SetConfig(config)
configureCache(config, projectDir)

inputDirectory := config.ProjectDirectory
outputDirectory := config.Modelsource
Expand Down Expand Up @@ -91,6 +94,7 @@ func main() {
}
lint.SetLogger(log)
lint.SetConfig(config)
configureCache(config, projectDir)

rulesDirectory := config.Rules.Path
modelDirectory := config.Modelsource
Expand All @@ -113,7 +117,7 @@ func main() {
config.Lint.XunitReport,
config.Lint.JSONFile,
boolValue(config.Lint.IgnoreNoqa, false),
!boolValue(config.Lint.NoCache, false),
boolValue(config.Cache.Enable, true),
)
if err != nil {
log.Errorf("lint failed: %s", err)
Expand Down Expand Up @@ -201,14 +205,27 @@ func main() {
Short: "Clear the lint results cache",
Long: "Removes all cached lint results. The cache is used to speed up repeated linting operations when rules and model files haven't changed.",
Run: func(cmd *cobra.Command, args []string) {
projectDir, err := os.Getwd()
if err != nil {
fmt.Printf("failed to resolve current working directory: %s\n", err)
os.Exit(1)
}
config, err := lint.LoadMergedConfigFromPath(projectDir, configPathForCommand(cmd))
if err != nil {
fmt.Printf("failed to load configuration: %s\n", err)
os.Exit(1)
}
lint.SetConfig(config)
configureCache(config, projectDir)

log := logrus.New()
if isVerbose(cmd) {
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(logrus.InfoLevel)
}
lint.SetLogger(log)
err := lint.ClearCache()
err = lint.ClearCache()
if err != nil {
log.Errorf("Failed to clear cache: %s", err)
os.Exit(1)
Expand All @@ -222,6 +239,19 @@ func main() {
Short: "Show cache statistics",
Long: "Displays information about the cached lint results, including number of entries and total size.",
Run: func(cmd *cobra.Command, args []string) {
projectDir, err := os.Getwd()
if err != nil {
fmt.Printf("failed to resolve current working directory: %s\n", err)
os.Exit(1)
}
config, err := lint.LoadMergedConfigFromPath(projectDir, configPathForCommand(cmd))
if err != nil {
fmt.Printf("failed to load configuration: %s\n", err)
os.Exit(1)
}
lint.SetConfig(config)
configureCache(config, projectDir)

log := logrus.New()
if isVerbose(cmd) {
log.SetLevel(logrus.DebugLevel)
Expand Down Expand Up @@ -255,6 +285,24 @@ func main() {
}
}

func configureCache(config *lint.Config, projectDir string) {
if config == nil {
return
}
cacheBase := strings.TrimSpace(config.Cache.Directory)
if cacheBase == "" {
return
}

if !filepath.IsAbs(cacheBase) {
cacheBase = filepath.Join(projectDir, cacheBase)
}

lint.SetCacheDirectory(filepath.Join(cacheBase, "lint"))
mpr.SetPersistentYAMLCacheDirectory(filepath.Join(cacheBase, "mpr-v2-yaml"))
mpr.SetPersistentYAMLCacheEnabled(boolValue(config.Cache.Enable, true))
}

func boolValue(value *bool, fallback bool) bool {
if value == nil {
return fallback
Expand Down
Loading
Loading