Skip to content

Commit

Permalink
chore: add cache in pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Nov 12, 2023
1 parent 5f3c0c6 commit cfb9b6c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 38 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,28 @@ jobs:
- 20.x
steps:
- uses: actions/checkout@v4

- name: Cache java-parser cloned repository
id: java-parser-cache-cloned-repository
uses: actions/cache@v3
with:
path: |
packages/java-parser/samples
key: ${{ runner.os }}-java-parser-cache-cloned-repository

- name: Cache Prettier Plugin Java cloned repository
id: prettier-plugin-java-cache-cloned-repository
uses: actions/cache@v3
with:
path: |
packages/prettier-plugin-java/samples
packages/prettier-plugin-java/test-samples
key: ${{ runner.os }}-prettier-plugin-java-cache-cloned-repository

- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
cache: 'yarn'
- uses: actions/setup-java@v3
with:
java-version: 17.x
Expand All @@ -36,9 +55,28 @@ jobs:
node_version: [18.x]
steps:
- uses: actions/checkout@v4

- name: Cache java-parser cloned repository
id: java-parser-cache-cloned-repository
uses: actions/cache@v3
with:
path: |
packages/java-parser/samples
key: ${{ runner.os }}-java-parser-cache-cloned-repository

- name: Cache Prettier Plugin Java cloned repository
id: prettier-plugin-java-cache-cloned-repository
uses: actions/cache@v3
with:
path: |
packages/prettier-plugin-java/samples
packages/prettier-plugin-java/test-samples
key: ${{ runner.os }}-prettier-plugin-java-cache-cloned-repository

- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
cache: 'yarn'
- uses: actions/setup-java@v3
with:
java-version: 17.x
Expand Down
55 changes: 36 additions & 19 deletions packages/java-parser/scripts/clone-samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"use strict";
const cp = require("child_process");
const path = require("path");
const fs = require("fs-extra");
const { existsSync } = require("fs");

const samplesDir = path.resolve(__dirname, "../samples");

Expand Down Expand Up @@ -87,25 +87,42 @@ const sampleRepos = [
}
];

fs.emptyDirSync(samplesDir);
sampleRepos.forEach(cloneRepoOrUpdateRepo);

sampleRepos.forEach(cloneRepo);
function cloneRepoOrUpdateRepo({ repoUrl, branch, commitHash }) {
if (!existsRepo({ repoUrl })) {
cloneRepo({ repoUrl, branch });
}

updateRepo({ repoUrl, branch, commitHash });
}

function repoDir(repoUrl) {
const repoName = repoUrl.split("/").pop().replace(".git", "");
return path.resolve(samplesDir, repoName);
}

function existsRepo({ repoUrl }) {
return existsSync(repoDir(repoUrl));
}

function cloneRepo({ repoUrl, branch, commitHash }) {
function cloneRepo({ repoUrl, branch }) {
console.log(`cloning ${repoUrl}`);
if (commitHash) {
cp.execSync(`git clone ${repoUrl} --branch ${branch}`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
cp.execSync(`git checkout ${commitHash}`, {
cwd: path.resolve(samplesDir, repoUrl.split("/").pop()),
stdio: [0, 1, 2]
});
} else {
cp.execSync(`git clone ${repoUrl} --branch ${branch} --depth 1`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
}
cp.execSync(`git clone ${repoUrl} --branch ${branch}`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
}

function updateRepo({ repoUrl, branch, commitHash }) {
console.log(`updating ${repoUrl}`);

const options = {
cwd: repoDir(repoUrl),
stdio: [0, 1, 2]
};
const sourceRef = commitHash ? commitHash : branch;

cp.execSync(`git fetch origin ${sourceRef}`, options);
cp.execSync(`git checkout ${sourceRef}`, options);
}
55 changes: 36 additions & 19 deletions packages/prettier-plugin-java/scripts/clone-samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"use strict";
const cp = require("child_process");
const path = require("path");
const fs = require("fs-extra");
const { existsSync } = require("fs");

const samplesDir = path.resolve(__dirname, "../samples");

Expand Down Expand Up @@ -85,25 +85,42 @@ if (process.argv.length === 3) {
}
}

fs.emptyDirSync(samplesDir);
sampleRepos.forEach(cloneRepoOrUpdateRepo);

sampleRepos.forEach(cloneRepo);
function cloneRepoOrUpdateRepo({ repoUrl, branch, commitHash }) {
if (!existsRepo({ repoUrl })) {
cloneRepo({ repoUrl, branch });
}

updateRepo({ repoUrl, branch, commitHash });
}

function repoDir(repoUrl) {
const repoName = repoUrl.split("/").pop().replace(".git", "");
return path.resolve(samplesDir, repoName);
}

function existsRepo({ repoUrl }) {
return existsSync(repoDir(repoUrl));
}

function cloneRepo({ repoUrl, branch, commitHash }) {
function cloneRepo({ repoUrl, branch }) {
console.log(`cloning ${repoUrl}`);
if (commitHash) {
cp.execSync(`git clone ${repoUrl} --branch ${branch}`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
cp.execSync(`git checkout ${commitHash}`, {
cwd: path.resolve(samplesDir, repoUrl.split("/").pop()),
stdio: [0, 1, 2]
});
} else {
cp.execSync(`git clone ${repoUrl} --branch ${branch} --depth 1`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
}
cp.execSync(`git clone ${repoUrl} --branch ${branch}`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
}

function updateRepo({ repoUrl, branch, commitHash }) {
console.log(`updating ${repoUrl}`);

const options = {
cwd: repoDir(repoUrl),
stdio: [0, 1, 2]
};
const sourceRef = commitHash ? commitHash : branch;

cp.execSync(`git fetch origin ${sourceRef}`, options);
cp.execSync(`git checkout ${sourceRef}`, options);
}

0 comments on commit cfb9b6c

Please sign in to comment.