Skip to content

Commit bb4772f

Browse files
authored
Merge pull request #2 from cloudfoundry/refactor
Refactor
2 parents c1b7bcf + 827d924 commit bb4772f

3 files changed

Lines changed: 68 additions & 102 deletions

File tree

build/build.go

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ type ModuleInstaller interface {
3131
InstallToLayer(string, string) error
3232
RebuildLayer(string, string) error
3333
CleanAndCopyToDst(string, string) error
34-
// WriteProfileD(string) error
35-
// WriteENV(string) error
3634
}
3735

3836
type Modules struct {
@@ -79,39 +77,43 @@ func (m Modules) Contribute() error {
7977
}
8078

8179
if m.buildContribution {
82-
m.cacheLayer.AppendPathEnv("NODE_PATH", filepath.Join(m.cacheLayer.Root, "node_modules"))
8380
if !m.launchContribution {
81+
m.logger.FirstLine("%s: %s to cache", logHeader(), color.YellowString("Contributing"))
8482
if err := m.installInCache(); err != nil {
85-
return fmt.Errorf("Failed to install in cache for build : %v", err)
83+
return fmt.Errorf("failed to install in cache for build : %v", err)
8684
}
8785
}
88-
}
89-
90-
if m.launchContribution {
91-
if err := m.writeProfile(); err != nil {
92-
return fmt.Errorf("Failed to write profile.d : %v", err)
93-
}
9486

95-
sameSHASums, err := m.packageLockMatchesMetadataSha()
96-
if err != nil {
87+
m.logger.SubsequentLine("Writing NODE_PATH")
88+
if err := m.cacheLayer.AppendPathEnv("NODE_PATH", filepath.Join(m.cacheLayer.Root, "node_modules")); err != nil {
9789
return err
9890
}
91+
}
9992

100-
if sameSHASums {
93+
if m.launchContribution {
94+
if sameSHASums, err := m.packageLockMatchesMetadataSha(); err != nil {
95+
return err
96+
} else if sameSHASums {
97+
m.logger.FirstLine("%s: %s cached launch layer", logHeader(), color.GreenString("Reusing"))
10198
return nil
10299
}
103100

101+
m.logger.FirstLine("%s: %s to launch", logHeader(), color.YellowString("Contributing"))
102+
104103
if err := m.installInCache(); err != nil {
105-
return fmt.Errorf("Failed to install in cache for launch : %v", err)
104+
return fmt.Errorf("failed to install in cache for launch : %v", err)
106105
}
107106

108107
if err := m.installInLaunch(); err != nil {
109-
return fmt.Errorf("Failed to install in launch : %v", err)
108+
return fmt.Errorf("failed to install in launch : %v", err)
109+
}
110+
111+
if err := m.writeProfile(); err != nil {
112+
return fmt.Errorf("failed to write profile.d : %v", err)
110113
}
111114
}
112115

113116
appModulesDir := filepath.Join(m.app.Root, "node_modules")
114-
m.logger.SubsequentLine("Removing node_modules from app")
115117
if err := os.RemoveAll(appModulesDir); err != nil {
116118
return fmt.Errorf("failed to clean up the node_modules: %v", err)
117119
}
@@ -127,36 +129,33 @@ func (m Modules) packageLockMatchesMetadataSha() (bool, error) {
127129
return false, fmt.Errorf("there is no package-lock.json in the app")
128130
}
129131

130-
packageLockSha := sha256.New()
131-
if buf, err := ioutil.ReadFile(packageLockPath); err != nil {
132-
return false, fmt.Errorf("failed to read metadata: %v", err)
133-
} else {
134-
packageLockSha.Write(buf)
132+
buf, err := ioutil.ReadFile(packageLockPath)
133+
if err != nil {
134+
return false, fmt.Errorf("failed to read package-lock.json: %v", err)
135135
}
136136

137137
var metadata Metadata
138-
m.launchLayer.ReadMetadata(&metadata)
138+
if err := m.launchLayer.ReadMetadata(&metadata); err != nil {
139+
return false, err
140+
}
141+
139142
metadataHash, err := hex.DecodeString(metadata.SHA256)
140143
if err != nil {
141144
return false, err
142145
}
143146

144-
return bytes.Equal(metadataHash, packageLockSha.Sum(nil)), nil
147+
hash := sha256.Sum256(buf)
148+
return bytes.Equal(metadataHash, hash[:]), nil
145149
}
146150

147151
func (m Modules) writeMetadataSha(path string) error {
148-
sha := sha256.New()
149-
if buf, err := ioutil.ReadFile(path); err != nil {
152+
buf, err := ioutil.ReadFile(path)
153+
if err != nil {
150154
return fmt.Errorf("failed to read %s: %v", path, err)
151-
} else {
152-
if _, err := sha.Write(buf); err != nil {
153-
return err
154-
}
155155
}
156156

157-
return m.launchLayer.WriteMetadata(Metadata{
158-
SHA256: hex.EncodeToString(sha.Sum(nil)),
159-
})
157+
hash := sha256.Sum256(buf)
158+
return m.launchLayer.WriteMetadata(Metadata{SHA256: hex.EncodeToString(hash[:])})
160159
}
161160

162161
func (m *Modules) copyModulesToLayer(src, dest string) error {
@@ -167,34 +166,30 @@ func (m *Modules) copyModulesToLayer(src, dest string) error {
167166
return err
168167
}
169168
}
170-
171-
if err := utils.CopyDirectory(src, dest); err != nil {
172-
return err
173-
}
174-
175-
return nil
169+
return utils.CopyDirectory(src, dest)
176170
}
177171

178172
func (m Modules) installInCache() error {
179173
appModulesDir := filepath.Join(m.app.Root, "node_modules")
174+
180175
vendored, err := libjavabuildpack.FileExists(appModulesDir)
181176
if err != nil {
182177
return fmt.Errorf("could not locate app modules directory : %s", err)
183178
}
184179

185180
if vendored {
186-
m.logger.SubsequentLine("%s cached node_modules", color.YellowString("Rebuilding"))
181+
m.logger.SubsequentLine("%s node_modules", color.YellowString("Rebuilding"))
182+
187183
if err := m.npm.RebuildLayer(m.app.Root, m.cacheLayer.Root); err != nil {
188184
return fmt.Errorf("failed to rebuild node_modules: %v", err)
189185
}
190186
} else {
191187
m.logger.SubsequentLine("%s node_modules", color.YellowString("Installing"))
192188

193189
cacheModulesDir := filepath.Join(m.cacheLayer.Root, "node_modules")
194-
195-
if exist, err := libjavabuildpack.FileExists(cacheModulesDir); err != nil {
190+
if exists, err := libjavabuildpack.FileExists(cacheModulesDir); err != nil {
196191
return err
197-
} else if !exist {
192+
} else if !exists {
198193
if err := os.MkdirAll(cacheModulesDir, 0777); err != nil {
199194
return fmt.Errorf("could not make node modules directory : %s", err)
200195
}
@@ -203,33 +198,17 @@ func (m Modules) installInCache() error {
203198
if err := os.Symlink(cacheModulesDir, appModulesDir); err != nil {
204199
return fmt.Errorf("could not symlink node modules directory : %s", err)
205200
}
206-
defer func() error {
207-
if err := os.Remove(appModulesDir); err != nil {
208-
return err
209-
}
210-
return nil
211-
}()
201+
defer os.Remove(appModulesDir)
212202

213203
if err := m.npm.InstallToLayer(m.app.Root, m.cacheLayer.Root); err != nil {
214204
return fmt.Errorf("failed to install and copy node_modules: %v", err)
215205
}
216-
217206
}
218207

219208
return nil
220209
}
221210

222211
func (m Modules) installInLaunch() error {
223-
sameSHASums, err := m.packageLockMatchesMetadataSha()
224-
if err != nil {
225-
return err
226-
}
227-
228-
if sameSHASums {
229-
m.logger.SubsequentLine("app and launch layers match.")
230-
return nil
231-
}
232-
233212
cacheModulesDir := filepath.Join(m.cacheLayer.Root, "node_modules")
234213
launchModulesDir := filepath.Join(m.launchLayer.Root, "node_modules")
235214

@@ -245,12 +224,15 @@ func (m Modules) installInLaunch() error {
245224
}
246225

247226
func (m Modules) writeProfile() error {
248-
launchModulesDir := filepath.Join(m.launchLayer.Root, "node_modules")
249-
250227
m.logger.SubsequentLine("Writing profile.d/NODE_PATH")
228+
229+
launchModulesDir := filepath.Join(m.launchLayer.Root, "node_modules")
251230
if err := m.launchLayer.WriteProfile("NODE_PATH", fmt.Sprintf("export NODE_PATH=%s", launchModulesDir)); err != nil {
252231
return fmt.Errorf("failed to write NODE_PATH in the launch layer: %v", err)
253232
}
254-
255233
return nil
256234
}
235+
236+
func logHeader() string {
237+
return color.New(color.FgBlue, color.Bold).Sprint("Node Modules")
238+
}

npm/npm.go

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,34 @@ type NPM struct {
1414
}
1515

1616
func NewNPM() *NPM {
17-
return &NPM{
18-
Runner: &npmCmd{},
19-
}
17+
return &NPM{Runner: &npmCmd{}}
2018
}
2119

2220
func (n *NPM) InstallToLayer(srcLayer, dstLayer string) error {
21+
srcPackageJsonPath := filepath.Join(srcLayer, "package.json")
22+
if exists, err := libjavabuildpack.FileExists(srcPackageJsonPath); err != nil || !exists {
23+
return fmt.Errorf("failed to find file %s ", srcPackageJsonPath)
24+
}
25+
26+
return n.Runner.Run(srcLayer, "install", "--unsafe-perm", "--cache", filepath.Join(srcLayer, "npm-cache"))
27+
}
2328

29+
func (n *NPM) RebuildLayer(srcLayer, dstLayer string) error {
2430
srcPackageJsonPath := filepath.Join(srcLayer, "package.json")
25-
_, err := os.Stat(srcPackageJsonPath)
26-
if err != nil {
27-
return fmt.Errorf("package.json file not found at %s with error %s", srcPackageJsonPath, err)
31+
if exists, err := libjavabuildpack.FileExists(srcPackageJsonPath); err != nil || !exists {
32+
return fmt.Errorf("failed to find file %s ", srcPackageJsonPath)
2833
}
2934

30-
if err = n.Runner.Run(
31-
srcLayer,
32-
"install",
33-
"--unsafe-perm",
34-
"--cache",
35-
fmt.Sprintf("%s/npm-cache", srcLayer),
36-
); err != nil {
35+
if err := n.Runner.Run(srcLayer, "rebuild"); err != nil {
3736
return err
3837
}
3938

39+
srcModulesDir := filepath.Join(srcLayer, "node_modules")
40+
dstModulesDir := filepath.Join(dstLayer, "node_modules")
41+
if err := n.CleanAndCopyToDst(srcModulesDir, dstModulesDir); err != nil {
42+
return fmt.Errorf("failed to rebuild : %v", err)
43+
}
44+
4045
return nil
4146
}
4247

@@ -48,41 +53,21 @@ func (n *NPM) CleanAndCopyToDst(src, dst string) error {
4853
if err := n.copyModules(src, dst); err != nil {
4954
return fmt.Errorf("failed to copy the src modules from %s to %s %v", src, dst, err)
5055
}
56+
5157
return nil
5258
}
5359

54-
func (n *NPM) RebuildLayer(srcLayer, dstLayer string) error {
55-
srcPackageJsonPath := filepath.Join(srcLayer, "package.json")
56-
_, err := os.Stat(srcPackageJsonPath)
60+
func (n *NPM) copyModules(src, dst string) error {
61+
exists, err := libjavabuildpack.FileExists(dst)
5762
if err != nil {
58-
return fmt.Errorf("package.json file not found at %s with error %s", srcPackageJsonPath, err)
59-
}
60-
61-
srcModulesDir := filepath.Join(srcLayer, "node_modules")
62-
dstModulesDir := filepath.Join(dstLayer, "node_modules")
63-
64-
if err := n.Runner.Run(srcLayer, "rebuild"); err != nil {
6563
return err
6664
}
6765

68-
if err := n.CleanAndCopyToDst(srcModulesDir, dstModulesDir); err != nil {
69-
return fmt.Errorf("failed to rebuild : %v", err)
70-
}
71-
return nil
72-
}
73-
74-
func (n *NPM) copyModules(src, dst string) error {
75-
if exist, err := libjavabuildpack.FileExists(dst); err != nil {
76-
return err
77-
} else if !exist {
66+
if !exists {
7867
if err := os.MkdirAll(dst, 0777); err != nil {
7968
return err
8069
}
8170
}
8271

83-
if err := utils.CopyDirectory(src, dst); err != nil {
84-
return err
85-
}
86-
87-
return nil
72+
return utils.CopyDirectory(src, dst)
8873
}

npm/npm_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package npm_test
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
76
"path/filepath"
@@ -110,7 +109,7 @@ func testNpm(t *testing.T, when spec.G, it spec.S) {
110109
})
111110

112111
it("run NPM install in the app dir", func() {
113-
installCommand := []string{"install", "--unsafe-perm", "--cache", fmt.Sprintf("%s/npm-cache", appRoot)}
112+
installCommand := []string{"install", "--unsafe-perm", "--cache", filepath.Join(appRoot, "npm-cache")}
114113
mockRunner.EXPECT().Run(appRoot, installCommand).Times(1)
115114

116115
err = Npm.InstallToLayer(appRoot, cacheLayer)

0 commit comments

Comments
 (0)