Skip to content

Commit 5429e5d

Browse files
committed
Use Dart Sass and the embedded compiler from HEAD by default
This matches the behavior of other CI tooling, and is low-risk since the release process releases the versions from HEAD anyway. It also eliminates the need to make empty version-only updates to the compiler just to make logic in Dart Sass visible to the test infrastructure. This also refactors the tool/ directory to move each individual `getX()` function into its own file, rather than lumping them all together in the ambiguously-named `utils.ts`. Any utils functions that are only used in one file have been moved to that file.
1 parent ec3c46d commit 5429e5d

File tree

10 files changed

+331
-423
lines changed

10 files changed

+331
-423
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,17 @@ jobs:
7676
with: {repo: sass/embedded-protocol, default-ref: null}
7777

7878
- name: Check out Dart Sass
79-
id: clone-dart-sass
8079
uses: sass/clone-linked-repo@v1
81-
with: {repo: sass/dart-sass, default-ref: null}
80+
with: {repo: sass/dart-sass}
8281

8382
- name: Check out the embedded compiler
8483
uses: sass/clone-linked-repo@v1
85-
with:
86-
repo: sass/dart-sass-embedded
87-
# If we check out a specific version of Dart Sass, always check out
88-
# the embedded compiler as well so we can actually use that Dart Sass
89-
# version.
90-
default-ref: ${{ !steps.clone-dart-sass.skip && 'main' || null }}
84+
with: {repo: sass/dart-sass-embedded}
9185

9286
- name: Link the embedded compiler to Dart Sass
9387
run: |
94-
if [[ -d dart-sass ]]; then
95-
yq -i '.dependency_overrides.sass = {"path": "../dart-sass"}' \
96-
dart-sass-embedded/pubspec.yaml
97-
fi
88+
yq -i '.dependency_overrides.sass = {"path": "../dart-sass"}' \
89+
dart-sass-embedded/pubspec.yaml
9890
9991
- name: Check out the JS API definition
10092
uses: sass/clone-linked-repo@v1
@@ -104,8 +96,7 @@ jobs:
10496
- name: npm run init
10597
run: |
10698
if [[ -d embedded-protocol ]]; then args=--protocol-path=embedded-protocol; fi
107-
if [[ -d dart-sass-embedded ]]; then args="$args --compiler-path=dart-sass-embedded"; fi
108-
npm run init -- --api-path=language $args
99+
npm run init -- --compiler-path=dart-sass-embedded --api-path=language $args
109100
110101
- run: npm run test
111102

@@ -146,25 +137,17 @@ jobs:
146137
with: {repo: sass/embedded-protocol, default-ref: null}
147138

148139
- name: Check out Dart Sass
149-
id: clone-dart-sass
150140
uses: sass/clone-linked-repo@v1
151-
with: {repo: sass/dart-sass, default-ref: null}
141+
with: {repo: sass/dart-sass}
152142

153143
- name: Check out the embedded compiler
154144
uses: sass/clone-linked-repo@v1
155-
with:
156-
repo: sass/dart-sass-embedded
157-
# If we check out a specific version of Dart Sass, always check out
158-
# the embedded compiler as well so we can actually use that Dart Sass
159-
# version.
160-
default-ref: ${{ !steps.clone-dart-sass.skip && 'main' || null }}
145+
with: {repo: sass/dart-sass-embedded}
161146

162147
- name: Link the embedded compiler to Dart Sass
163148
run: |
164-
if [[ -d dart-sass ]]; then
165-
yq -i '.dependency_overrides.sass = {"path": "../dart-sass"}' \
166-
dart-sass-embedded/pubspec.yaml
167-
fi
149+
yq -i '.dependency_overrides.sass = {"path": "../dart-sass"}' \
150+
dart-sass-embedded/pubspec.yaml
168151
169152
- name: Check out the JS API definition
170153
uses: sass/clone-linked-repo@v1
@@ -174,8 +157,7 @@ jobs:
174157
- name: npm run init
175158
run: |
176159
if [[ -d embedded-protocol ]]; then args=--protocol-path=embedded-protocol; fi
177-
if [[ -d dart-sass-embedded ]]; then args="$args --compiler-path=dart-sass-embedded"; fi
178-
npm run init -- --api-path=language $args
160+
npm run init -- --compiler-path=dart-sass-embedded --api-path=language $args
179161
180162
- name: Check out sass-spec
181163
uses: sass/clone-linked-repo@v1

CONTRIBUTING.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,18 @@ JS API):
6767
* `--<type>-ref`: A Git reference for the GitHub repository of the package to
6868
clone.
6969

70-
* `--<type>-version`: The released version of the package to use. This isn't
71-
available for `api`, because the JS API definition doesn't have its own tagged
72-
versions.
73-
7470
By default:
7571

7672
* This uses the version of the embedded protocol and compiler specified by
77-
`protocol-version` and `compiler-version` in `package.json`, *unless* these
78-
versions end in `-dev` in which case it checks out the latest revision on
79-
GitHub.
80-
81-
* This uses the JS API definition from the latest revision on GitHub.
82-
83-
`npm run init` chooses the Dart Sass version as follows:
84-
85-
* If a released version of the embedded compiler was specified, it uses the
86-
version of Dart Sass compiled into that release.
73+
`protocol-version` in `package.json`, *unless* that version ends in `-dev` in
74+
which case it checks out the latest revision on GitHub.
8775

88-
* If `--compiler-path` was specified, it uses the version of Dart Sass linked to
89-
that compiler.
76+
* This uses the embedded compiler version and JS API definition from the latest
77+
revision on GitHub.
9078

91-
* If the embedded compiler was cloned from GitHub, it uses the version of Dart
92-
Sass specified in its pubspec *unless* that version ends in `-dev`, in which
93-
case it checks out the latest revision on GitHub.
79+
* This uses the Dart Sass version from the latest revision on GitHub, unless the
80+
embedded `--compiler-path` was passed in which case it uses whatever version
81+
of Dart Sass that package references.
9482

9583
## Continuous Integration
9684

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"node": ">=14.0.0"
1717
},
1818
"scripts": {
19-
"init": "ts-node ./tool/prepare-dev-environment.ts",
19+
"init": "ts-node ./tool/init.ts",
2020
"check": "npm-run-all check:gts check:tsc",
2121
"check:gts": "gts check",
2222
"check:tsc": "tsc --noEmit",

tool/get-embedded-compiler.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2022 Google Inc. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
import {promises as fs} from 'fs';
6+
import * as p from 'path';
7+
import * as yaml from 'yaml';
8+
import * as shell from 'shelljs';
9+
10+
import * as utils from './utils';
11+
12+
/**
13+
* Downlaods and builds the Embedded Dart Sass compiler.
14+
*
15+
* Can check out and build the source from a Git `ref` or build from the source
16+
* at `path`. By default, checks out the latest revision from GitHub.
17+
*/
18+
export async function getEmbeddedCompiler(
19+
outPath: string,
20+
options?: {ref: string} | {path: string}
21+
): Promise<void> {
22+
const repo = 'dart-sass-embedded';
23+
24+
let source: string;
25+
if (!options || 'ref' in options) {
26+
utils.fetchRepo({
27+
repo,
28+
outPath: utils.BUILD_PATH,
29+
ref: options?.ref ?? 'main',
30+
});
31+
source = p.join(utils.BUILD_PATH, repo);
32+
await maybeOverrideSassDependency(source);
33+
} else {
34+
source = options.path;
35+
}
36+
37+
buildDartSassEmbedded(source);
38+
await utils.link(p.join(source, 'build'), p.join(outPath, repo));
39+
}
40+
41+
/**
42+
* Overrides Embedded Dart Sass compiler's dependency on Dart Sass to use the
43+
* latest version of Dart Sass from the `main` branch.
44+
*
45+
* This allows us to avoid needing to commit a dependency override to the
46+
* embedded compiler when it doesn't actually require any local changes.
47+
*/
48+
async function maybeOverrideSassDependency(repo: string): Promise<void> {
49+
const pubspecPath = p.join(repo, 'pubspec.yaml');
50+
const pubspec = yaml.parse(
51+
await fs.readFile(pubspecPath, {encoding: 'utf-8'})
52+
);
53+
54+
console.log(`Overriding ${repo} to load Dart Sass from HEAD.`);
55+
56+
pubspec['dependency_overrides'] = {
57+
...pubspec['dependency_overrides'],
58+
sass: {git: 'https://github.com/sass/dart-sass.git'},
59+
};
60+
await fs.writeFile(pubspecPath, yaml.stringify(pubspec), {encoding: 'utf-8'});
61+
}
62+
63+
// Builds the Embedded Dart Sass executable from the source at `repoPath`.
64+
function buildDartSassEmbedded(repoPath: string): void {
65+
console.log('Downloading dart-sass-embedded dependencies.');
66+
shell.exec('dart pub upgrade', {
67+
cwd: repoPath,
68+
silent: true,
69+
});
70+
71+
console.log('Building dart-sass-embedded executable.');
72+
shell.exec('dart run grinder protobuf pkg-standalone-dev', {
73+
cwd: repoPath,
74+
silent: true,
75+
});
76+
}

tool/get-embedded-protocol.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 Google Inc. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
import {mkdirSync} from 'fs';
6+
import * as p from 'path';
7+
import * as shell from 'shelljs';
8+
9+
import * as pkg from '../package.json';
10+
import * as utils from './utils';
11+
12+
/**
13+
* Downloads and builds the Embedded Sass protocol definition.
14+
*
15+
* Can check out and build the source from a Git `ref` or build from the source
16+
* at `path`. By default, checks out the tagged version specified in
17+
* package.json's `protocol-version` field. If this version ends in `-dev`,
18+
* checks out the latest revision from GitHub instead.
19+
*/
20+
export async function getEmbeddedProtocol(
21+
outPath: string,
22+
options?: {ref: string} | {path: string}
23+
): Promise<void> {
24+
const repo = 'embedded-protocol';
25+
26+
let source: string;
27+
if (!options || 'ref' in options) {
28+
let ref = options?.ref;
29+
if (ref === undefined) {
30+
const version = pkg['protocol-version'] as string;
31+
ref = version.endsWith('-dev') ? 'main' : version;
32+
}
33+
34+
utils.fetchRepo({repo, outPath: utils.BUILD_PATH, ref});
35+
source = p.join(utils.BUILD_PATH, repo);
36+
} else {
37+
source = options.path;
38+
}
39+
40+
buildEmbeddedProtocol(source);
41+
await utils.link('build/embedded-protocol', p.join(outPath, repo));
42+
}
43+
44+
// Builds the embedded proto at `repoPath` into a pbjs with TS declaration file.
45+
function buildEmbeddedProtocol(repoPath: string): void {
46+
const proto = p.join(repoPath, 'embedded_sass.proto');
47+
const protocPath =
48+
process.platform === 'win32'
49+
? '%CD%/node_modules/protoc/protoc/bin/protoc.exe'
50+
: 'node_modules/protoc/protoc/bin/protoc';
51+
const version = shell
52+
.exec(`${protocPath} --version`, {silent: true})
53+
.stdout.trim();
54+
console.log(
55+
`Building pbjs and TS declaration file from ${proto} with ${version}.`
56+
);
57+
58+
const pluginPath =
59+
process.platform === 'win32'
60+
? '%CD%/node_modules/.bin/protoc-gen-ts.cmd'
61+
: 'node_modules/.bin/protoc-gen-ts';
62+
mkdirSync('build/embedded-protocol', {recursive: true});
63+
shell.exec(
64+
`${protocPath} \
65+
--plugin="protoc-gen-ts=${pluginPath}" \
66+
--js_out="import_style=commonjs,binary:build/embedded-protocol" \
67+
--ts_out="build/embedded-protocol" \
68+
--proto_path="${repoPath}" \
69+
${proto}`,
70+
{silent: true}
71+
);
72+
}

tool/get-js-api.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 Google Inc. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
import * as p from 'path';
6+
7+
import * as utils from './utils';
8+
9+
/**
10+
* Checks out JS API type definitions from the Sass language repo.
11+
*
12+
* Can check out a Git `ref` or link to the source at `path`. By default, checks
13+
* out the latest revision from GitHub.
14+
*/
15+
export async function getJSApi(
16+
outPath: string,
17+
options?: {ref: string} | {path: string}
18+
): Promise<void> {
19+
const repo = 'sass';
20+
21+
let source: string;
22+
if (!options || 'ref' in options) {
23+
utils.fetchRepo({
24+
repo,
25+
outPath: utils.BUILD_PATH,
26+
ref: options?.ref ?? 'main',
27+
});
28+
source = p.join(utils.BUILD_PATH, repo);
29+
} else {
30+
source = options.path;
31+
}
32+
33+
await utils.link(p.join(source, 'js-api-doc'), p.join(outPath, repo));
34+
}

0 commit comments

Comments
 (0)