Skip to content

Commit 0ed94a5

Browse files
authored
Add SBT in cache managers. (actions#302)
1 parent 9519cf1 commit 0ed94a5

File tree

9 files changed

+204
-1
lines changed

9 files changed

+204
-1
lines changed

.github/workflows/e2e-cache.yml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,99 @@ jobs:
111111
exit 1
112112
fi
113113
ls ~/.m2/repository
114+
sbt-save:
115+
runs-on: ${{ matrix.os }}
116+
defaults:
117+
run:
118+
shell: bash
119+
working-directory: __tests__/cache/sbt
120+
strategy:
121+
fail-fast: false
122+
matrix:
123+
os: [macos-latest, windows-latest, ubuntu-latest]
124+
steps:
125+
- name: Checkout
126+
uses: actions/checkout@v3
127+
- name: Run setup-java with the cache for sbt
128+
uses: ./
129+
id: setup-java
130+
with:
131+
distribution: 'adopt'
132+
java-version: '11'
133+
cache: sbt
134+
- name: Create files to cache
135+
run: sbt update
136+
137+
- name: Check files to cache on macos-latest
138+
if: matrix.os == 'macos-latest'
139+
run: |
140+
if [ ! -d ~/Library/Caches/Coursier ]; then
141+
echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
142+
exit 1
143+
fi
144+
145+
- name: Check files to cache on windows-latest
146+
if: matrix.os == 'windows-latest'
147+
run: |
148+
if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
149+
echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
150+
exit 1
151+
fi
152+
153+
154+
- name: Check files to cache on ubuntu-latest
155+
if: matrix.os == 'ubuntu-latest'
156+
run: |
157+
if [ ! -d ~/.cache/coursier ]; then
158+
echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
159+
exit 1
160+
fi
161+
162+
sbt-restore:
163+
runs-on: ${{ matrix.os }}
164+
defaults:
165+
run:
166+
shell: bash
167+
working-directory: __tests__/cache/sbt
168+
strategy:
169+
fail-fast: false
170+
matrix:
171+
os: [macos-latest, windows-latest, ubuntu-latest]
172+
needs: sbt-save
173+
steps:
174+
- name: Checkout
175+
uses: actions/checkout@v3
176+
- name: Run setup-java with the cache for sbt
177+
uses: ./
178+
id: setup-java
179+
with:
180+
distribution: 'adopt'
181+
java-version: '11'
182+
cache: sbt
183+
184+
- name: Confirm that ~/Library/Caches/Coursier directory has been made
185+
if: matrix.os == 'macos-latest'
186+
run: |
187+
if [ ! -d ~/Library/Caches/Coursier ]; then
188+
echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
189+
exit 1
190+
fi
191+
ls ~/Library/Caches/Coursier
192+
193+
- name: Confirm that ~/AppData/Local/Coursier/Cache directory has been made
194+
if: matrix.os == 'windows-latest'
195+
run: |
196+
if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
197+
echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
198+
exit 1
199+
fi
200+
ls ~/AppData/Local/Coursier/Cache
201+
202+
- name: Confirm that ~/.cache/coursier directory has been made
203+
if: matrix.os == 'ubuntu-latest'
204+
run: |
205+
if [ ! -d ~/.cache/coursier ]; then
206+
echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
207+
exit 1
208+
fi
209+
ls ~/.cache/coursier

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Currently, the following distributions are supported:
6969
The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are gradle and maven. The format of the used cache key is `setup-java-${{ platform }}-${{ packageManager }}-${{ fileHash }}`, where the hash is based on the following files:
7070
- gradle: `**/*.gradle*`, `**/gradle-wrapper.properties`
7171
- maven: `**/pom.xml`
72+
- sbt: `**/build.sbt`
7273

7374
The workflow output `cache-hit` is set to indicate if an exact match was found for the key [as actions/cache does](https://github.com/actions/cache/tree/main#outputs).
7475

@@ -99,6 +100,19 @@ steps:
99100
run: mvn -B package --file pom.xml
100101
```
101102

103+
#### Caching sbt dependencies
104+
```yaml
105+
steps:
106+
- uses: actions/checkout@v3
107+
- uses: actions/setup-java@v3
108+
with:
109+
distribution: 'temurin'
110+
java-version: '11'
111+
cache: 'sbt'
112+
- name: Build with SBT
113+
run: sbt package
114+
```
115+
102116
### Check latest
103117
In the basic examples above, the `check-latest` flag defaults to `false`. When set to `false`, the action tries to first resolve a version of Java from the local tool cache on the runner. If unable to find a specific version in the cache, the action will download a version of Java. Use the default or set `check-latest` to `false` if you prefer a faster more consistent setup experience that prioritizes trying to use the cached versions at the expense of newer versions sometimes being available for download.
104118

__tests__/cache.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ describe('dependency cache', () => {
118118
expect(spyInfo).toBeCalledWith('gradle cache is not found');
119119
});
120120
});
121+
describe('for sbt', () => {
122+
it('throws error if no build.sbt found', async () => {
123+
await expect(restore('sbt')).rejects.toThrowError(
124+
`No file in ${projectRoot(
125+
workspace
126+
)} matched to [**/*.sbt,**/project/build.properties,**/project/**.{scala,sbt}], make sure you have checked out the target repository`
127+
);
128+
});
129+
it('downloads cache', async () => {
130+
createFile(join(workspace, 'build.sbt'));
131+
132+
await restore('sbt');
133+
expect(spyCacheRestore).toBeCalled();
134+
expect(spyWarning).not.toBeCalled();
135+
expect(spyInfo).toBeCalledWith('sbt cache is not found');
136+
});
137+
});
121138
});
122139
describe('save', () => {
123140
let spyCacheSave: jest.SpyInstance<
@@ -194,6 +211,30 @@ describe('dependency cache', () => {
194211
expect(spyInfo).toBeCalledWith(expect.stringMatching(/^Cache saved with the key:.*/));
195212
});
196213
});
214+
describe('for sbt', () => {
215+
it('uploads cache even if no build.sbt found', async () => {
216+
createStateForMissingBuildFile();
217+
await save('sbt');
218+
expect(spyCacheSave).toBeCalled();
219+
expect(spyWarning).not.toBeCalled();
220+
});
221+
it('does not upload cache if no restore run before', async () => {
222+
createFile(join(workspace, 'build.sbt'));
223+
224+
await save('sbt');
225+
expect(spyCacheSave).not.toBeCalled();
226+
expect(spyWarning).toBeCalledWith('Error retrieving key from state.');
227+
});
228+
it('uploads cache', async () => {
229+
createFile(join(workspace, 'build.sbt'));
230+
createStateForSuccessfulRestore();
231+
232+
await save('sbt');
233+
expect(spyCacheSave).toBeCalled();
234+
expect(spyWarning).not.toBeCalled();
235+
expect(spyInfo).toBeCalledWith(expect.stringMatching(/^Cache saved with the key:.*/));
236+
});
237+
});
197238
});
198239
});
199240

__tests__/cache/sbt/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

__tests__/cache/sbt/build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ThisBuild / scalaVersion := "2.12.15"
2+
3+
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.6.2

dist/cleanup/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63300,8 +63300,24 @@ const supportedPackageManager = [
6330063300
path: [path_1.join(os_1.default.homedir(), '.gradle', 'caches'), path_1.join(os_1.default.homedir(), '.gradle', 'wrapper')],
6330163301
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---gradle
6330263302
pattern: ['**/*.gradle*', '**/gradle-wrapper.properties']
63303+
},
63304+
{
63305+
id: 'sbt',
63306+
path: [
63307+
path_1.join(os_1.default.homedir(), '.ivy2', 'cache'),
63308+
path_1.join(os_1.default.homedir(), '.sbt'),
63309+
getCoursierCachePath()
63310+
],
63311+
pattern: ['**/*.sbt', '**/project/build.properties', '**/project/**.{scala,sbt}']
6330363312
}
6330463313
];
63314+
function getCoursierCachePath() {
63315+
if (os_1.default.type() === 'Linux')
63316+
return path_1.join(os_1.default.homedir(), '.cache', 'coursier');
63317+
if (os_1.default.type() === 'Darwin')
63318+
return path_1.join(os_1.default.homedir(), 'Library', 'Caches', 'Coursier');
63319+
return path_1.join(os_1.default.homedir(), 'AppData', 'Local', 'Coursier', 'Cache');
63320+
}
6330563321
function findPackageManager(id) {
6330663322
const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id);
6330763323
if (packageManager === undefined) {

dist/setup/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18632,8 +18632,24 @@ const supportedPackageManager = [
1863218632
path: [path_1.join(os_1.default.homedir(), '.gradle', 'caches'), path_1.join(os_1.default.homedir(), '.gradle', 'wrapper')],
1863318633
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---gradle
1863418634
pattern: ['**/*.gradle*', '**/gradle-wrapper.properties']
18635+
},
18636+
{
18637+
id: 'sbt',
18638+
path: [
18639+
path_1.join(os_1.default.homedir(), '.ivy2', 'cache'),
18640+
path_1.join(os_1.default.homedir(), '.sbt'),
18641+
getCoursierCachePath()
18642+
],
18643+
pattern: ['**/*.sbt', '**/project/build.properties', '**/project/**.{scala,sbt}']
1863518644
}
1863618645
];
18646+
function getCoursierCachePath() {
18647+
if (os_1.default.type() === 'Linux')
18648+
return path_1.join(os_1.default.homedir(), '.cache', 'coursier');
18649+
if (os_1.default.type() === 'Darwin')
18650+
return path_1.join(os_1.default.homedir(), 'Library', 'Caches', 'Coursier');
18651+
return path_1.join(os_1.default.homedir(), 'AppData', 'Local', 'Coursier', 'Cache');
18652+
}
1863718653
function findPackageManager(id) {
1863818654
const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id);
1863918655
if (packageManager === undefined) {

src/cache.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const CACHE_MATCHED_KEY = 'cache-matched-key';
1313
const CACHE_KEY_PREFIX = 'setup-java';
1414

1515
interface PackageManager {
16-
id: 'maven' | 'gradle';
16+
id: 'maven' | 'gradle' | 'sbt';
1717
/**
1818
* Paths of the file that specify the files to cache.
1919
*/
@@ -32,9 +32,24 @@ const supportedPackageManager: PackageManager[] = [
3232
path: [join(os.homedir(), '.gradle', 'caches'), join(os.homedir(), '.gradle', 'wrapper')],
3333
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---gradle
3434
pattern: ['**/*.gradle*', '**/gradle-wrapper.properties']
35+
},
36+
{
37+
id: 'sbt',
38+
path: [
39+
join(os.homedir(), '.ivy2', 'cache'),
40+
join(os.homedir(), '.sbt'),
41+
getCoursierCachePath()
42+
],
43+
pattern: ['**/*.sbt', '**/project/build.properties', '**/project/**.{scala,sbt}']
3544
}
3645
];
3746

47+
function getCoursierCachePath(): string {
48+
if (os.type() === 'Linux') return join(os.homedir(), '.cache', 'coursier');
49+
if (os.type() === 'Darwin') return join(os.homedir(), 'Library', 'Caches', 'Coursier');
50+
return join(os.homedir(), 'AppData', 'Local', 'Coursier', 'Cache');
51+
}
52+
3853
function findPackageManager(id: string): PackageManager {
3954
const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id);
4055
if (packageManager === undefined) {

0 commit comments

Comments
 (0)