add per-OS file variants via files.<os>/ directories

This commit is contained in:
2026-03-11 10:04:37 +00:00
parent 0215a53fcf
commit 5e9c4b2172
5 changed files with 376 additions and 74 deletions
+45 -11
View File
@@ -10,7 +10,7 @@ import (
"sigil/internal/core"
)
const version = "0.1.3"
const version = "0.2.0"
func main() {
if len(os.Args) < 2 {
@@ -111,18 +111,30 @@ func applyCmd(args []string) error {
}
filesDir := filepath.Join(pkgDir, core.FilesDirName)
if _, err := os.Stat(filesDir); err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return err
variantDir := filepath.Join(pkgDir, core.VariantDirName())
filesDirs := []string{filesDir}
if _, err := os.Stat(variantDir); err == nil {
filesDirs = append(filesDirs, variantDir)
}
if err := core.ApplyPackage(filesDir, targetRoot, cfg); err != nil {
// Check if any files dir exists
hasFiles := false
for _, dir := range filesDirs {
if _, err := os.Stat(dir); err == nil {
hasFiles = true
break
}
}
if !hasFiles {
continue
}
if err := core.ApplyPackages(filesDirs, targetRoot, cfg); err != nil {
return fmt.Errorf("%s: %w", entry.Name(), err)
}
stale, err := core.FindStaleLinks(filesDir, targetRoot)
stale, err := core.FindStaleLinksMulti(filesDirs, targetRoot)
if err != nil {
return fmt.Errorf("%s: %w", entry.Name(), err)
}
@@ -243,6 +255,8 @@ func addCmd(args []string) error {
return err
}
}
variantDir := filepath.Join(pkgDir, core.VariantDirName())
if info.IsDir() {
if pkgExists {
return errors.New("cannot merge a directory into an existing package yet")
@@ -259,7 +273,15 @@ func addCmd(args []string) error {
return fmt.Errorf("path %s is outside target %s", absPath, targetRoot)
}
destPath := filepath.Join(filesDir, rel)
// Smart default: check if variant file already exists
destDir := filesDir
variantPath := filepath.Join(variantDir, rel)
if _, err := os.Stat(variantPath); err == nil {
// Variant file exists, update it instead
destDir = variantDir
}
destPath := filepath.Join(destDir, rel)
if err := os.MkdirAll(filepath.Dir(destPath), 0o755); err != nil {
return err
}
@@ -278,7 +300,12 @@ func addCmd(args []string) error {
return err
}
return core.ApplyPackage(filesDir, targetRoot, cfg)
filesDirs := []string{filesDir}
if _, err := os.Stat(variantDir); err == nil {
filesDirs = append(filesDirs, variantDir)
}
return core.ApplyPackages(filesDirs, targetRoot, cfg)
}
func unlinkCmd(args []string) error {
@@ -325,7 +352,14 @@ func statusCmd() error {
}
filesDir := filepath.Join(pkgDir, core.FilesDirName)
stale, err := core.FindStaleLinks(filesDir, targetRoot)
variantDir := filepath.Join(pkgDir, core.VariantDirName())
filesDirs := []string{filesDir}
if _, err := os.Stat(variantDir); err == nil {
filesDirs = append(filesDirs, variantDir)
}
stale, err := core.FindStaleLinksMulti(filesDirs, targetRoot)
if err != nil {
return fmt.Errorf("%s: %w", entry.Name(), err)
}