Skip to content

Commit b6d7128

Browse files
committed
fix: deletion of composer.lock file, fixes #536
1 parent 4dd8417 commit b6d7128

1 file changed

Lines changed: 36 additions & 30 deletions

File tree

internal/verifier/rector.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,37 @@ func (r Rector) Fix(ctx context.Context, config ToolConfig) error {
2424
return nil
2525
}
2626

27-
composerJSONPath := path.Join(config.RootDir, "composer.json")
27+
_, err := os.Stat(path.Join(config.RootDir, "vendor"))
28+
vendorExists := !os.IsNotExist(err)
29+
2830
var backupData []byte
29-
if _, err := os.Stat(composerJSONPath); err == nil {
30-
backupData, err = os.ReadFile(composerJSONPath)
31-
if err != nil {
32-
return fmt.Errorf("failed to backup composer.json: %w", err)
31+
var composerLockData []byte
32+
33+
if !vendorExists {
34+
composerJSONPath := path.Join(config.RootDir, "composer.json")
35+
36+
if _, err := os.Stat(composerJSONPath); err == nil {
37+
backupData, err = os.ReadFile(composerJSONPath)
38+
if err != nil {
39+
return fmt.Errorf("failed to backup composer.json: %w", err)
40+
}
3341
}
34-
}
3542

36-
// Check and remove existing vendor/composer.lock
37-
vendorPath := path.Join(config.RootDir, "vendor")
38-
composerLockPath := path.Join(config.RootDir, "composer.lock")
43+
composerLockPath := path.Join(config.RootDir, "composer.lock")
44+
if _, err := os.Stat(composerLockPath); err == nil {
45+
composerLockData, err = os.ReadFile(composerLockPath)
46+
if err != nil {
47+
return fmt.Errorf("failed to backup composer.lock: %w", err)
48+
}
49+
}
3950

40-
if _, err := os.Stat(vendorPath); err == nil {
41-
if err := os.RemoveAll(vendorPath); err != nil {
42-
return err
51+
if _, err := os.Stat(composerLockPath); err == nil {
52+
if err := os.Remove(composerLockPath); err != nil {
53+
return err
54+
}
4355
}
44-
}
45-
if _, err := os.Stat(composerLockPath); err == nil {
46-
if err := os.Remove(composerLockPath); err != nil {
56+
57+
if err := installComposerDeps(config.RootDir, "highest"); err != nil {
4758
return err
4859
}
4960
}
@@ -56,10 +67,6 @@ func (r Rector) Fix(ctx context.Context, config ToolConfig) error {
5667
rectorConfigFile = path.Join(config.ToolDirectory, "php", "vendor", "frosh", "shopware-rector", "config", fmt.Sprintf("shopware-%s.0.php", config.MinShopwareVersion[0:3]))
5768
}
5869

59-
if err := installComposerDeps(config.RootDir, "highest"); err != nil {
60-
return err
61-
}
62-
6370
for _, sourceDirectory := range config.SourceDirectories {
6471
rector := exec.CommandContext(ctx, "php", "-dmemory_limit=2G", path.Join(config.ToolDirectory, "php", "vendor", "bin", "rector"), "process", "--config", rectorConfigFile, "--autoload-file", path.Join("vendor", "autoload.php"), sourceDirectory)
6572
rector.Dir = config.RootDir
@@ -69,18 +76,17 @@ func (r Rector) Fix(ctx context.Context, config ToolConfig) error {
6976
fmt.Print(string(log))
7077
}
7178

72-
// Cleanup after execution
73-
if err := os.RemoveAll(vendorPath); err != nil {
74-
return err
75-
}
76-
if err := os.Remove(composerLockPath); err != nil {
77-
return err
78-
}
79+
if !vendorExists {
80+
if backupData != nil {
81+
if err := os.WriteFile(path.Join(config.RootDir, "composer.json"), backupData, 0o644); err != nil {
82+
return fmt.Errorf("failed to restore composer.json: %w", err)
83+
}
84+
}
7985

80-
// Restore composer.json
81-
if backupData != nil {
82-
if err := os.WriteFile(composerJSONPath, backupData, 0o644); err != nil {
83-
return fmt.Errorf("failed to restore composer.json: %w", err)
86+
if composerLockData != nil {
87+
if err := os.WriteFile(path.Join(config.RootDir, "composer.lock"), composerLockData, 0o644); err != nil {
88+
return fmt.Errorf("failed to restore composer.lock: %w", err)
89+
}
8490
}
8591
}
8692

0 commit comments

Comments
 (0)