Skip to content

Commit 9fd181c

Browse files
committed
docs(gradle-api): allow absolute facts paths where no relative path exists
path.relative returns an absolute path when the target is on another Windows drive than the facts file. toFactsPath now writes that case as an absolute path with forward slashes on purpose, and the schema, README and type docs say paths are relative when possible and absolute otherwise, which the reader already supports. Also describe a plugin's `aar` as the path the CLI builds its aar to: a plugin without sources builds nothing there but may ship a file at it.
1 parent 8769ccf commit 9fd181c

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

‎gradle-api/README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,11 @@ built. The schema is [`schema/nativescript-gradle.schema.json`](schema/nativescr
372372
}
373373
```
374374

375-
- Paths are relative to the file, with forward slashes, so the folder can move.
375+
- Paths use forward slashes and are relative to the file, so the folder can move. When there is no
376+
relative path (on Windows, a project on another drive than the file) the path is absolute, e.g.
377+
`D:/app/node_modules/x`. The library handles both.
378+
- `aar` is where the CLI puts the `.aar` it builds from the plugin's sources. A plugin without
379+
sources builds nothing, but may ship a file at that path itself.
376380
- `minReaderSchemaVersion` is the lowest `schemaVersion` a reader must understand to use the file,
377381
never above the file's own `schemaVersion`.
378382
A library that supports only older schema versions refuses it (see [Errors](#errors)) instead of
@@ -393,7 +397,7 @@ These are what keep old plugins working on new CLIs, and new plugins working on
393397
`schemaVersion` goes up when fields are added; readers accept any version and read what they know,
394398
as long as it isn't below the file's `minReaderSchemaVersion`. A CLI raises `minReaderSchemaVersion`
395399
only for a change that old readers would get wrong if they ignored it, which should be never: it is
396-
the one way to tell libraries already published "you can't read this". Paths are relative. Anything a newer CLI does differently is expressed as data in the file, not as a
400+
the one way to tell libraries already published "you can't read this". Paths are relative where possible. Anything a newer CLI does differently is expressed as data in the file, not as a
397401
rule readers must know. A facts file that doesn't match the `dependencies.json` next to it is an
398402
error, never used.
399403

‎gradle-api/schema/nativescript-gradle.schema.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
33
"$id": "https://nativescript.org/schemas/nativescript-gradle.schema.json",
44
"title": "NativeScript Gradle facts",
5-
"description": "Written by the NativeScript CLI at prepare time to platforms/android/nativescript-gradle.json and read by org.nativescript:gradle-api. Fields are only ever added: readers must ignore unknown fields, and a field never changes meaning. Every path is relative to the directory containing this file and uses forward slashes.",
5+
"description": "Written by the NativeScript CLI at prepare time to platforms/android/nativescript-gradle.json and read by org.nativescript:gradle-api. Fields are only ever added: readers must ignore unknown fields, and a field never changes meaning. Every path uses forward slashes and is relative to the directory containing this file, or absolute when no relative path exists (on Windows, a target on another drive). Readers resolve relative paths against that directory and take absolute ones as they are.",
66
"type": "object",
77
"required": ["schemaVersion", "minReaderSchemaVersion", "dependenciesJson", "project", "plugins"],
88
"properties": {
@@ -67,6 +67,7 @@
6767
},
6868
"$defs": {
6969
"path": {
70+
"description": "Forward slashes only. Relative to the directory containing this file, or absolute (e.g. C:/app) when no relative path exists.",
7071
"type": "string",
7172
"pattern": "^[^\\\\]*$"
7273
},
@@ -91,7 +92,7 @@
9192
"type": "string"
9293
},
9394
"aar": {
94-
"description": "Where the CLI puts that aar. The file only exists if the plugin has sources to build.",
95+
"description": "Where the CLI puts the aar it builds from the plugin's sources. Says nothing about whether a file is there: a plugin without sources builds nothing, but may ship a file at this path itself.",
9596
"$ref": "#/$defs/path"
9697
},
9798
"dependencies": {

‎lib/definitions/gradle-facts.d.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* Contents of platforms/android/nativescript-gradle.json, read by
33
* org.nativescript:gradle-api. Mirrors gradle-api/schema/nativescript-gradle.schema.json:
4-
* fields are only ever added, and every path is relative to the file's
5-
* directory with forward slashes.
4+
* fields are only ever added, and every path uses forward slashes and is
5+
* relative to the file's directory, or absolute when no relative path exists
6+
* (another drive on Windows).
67
*/
78
export interface INativeScriptGradleFacts {
89
schemaVersion: number;

‎lib/services/android/gradle-facts.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export interface IGradleFactsPluginInput {
1919

2020
export interface IGradleFactsInput {
2121
/**
22-
* Directory of the facts file; every path in the facts is relative to it.
22+
* Directory of the facts file; paths in the facts are relative to it when
23+
* there is a relative path to them.
2324
*/
2425
factsDir: string;
2526
cliVersion: string;
@@ -37,8 +38,20 @@ export interface IGradleFactsInput {
3738
plugins: IGradleFactsPluginInput[];
3839
}
3940

40-
export function toFactsPath(factsDir: string, target: string): string {
41-
return path.relative(factsDir, target).split(path.sep).join("/") || ".";
41+
/**
42+
* `target` relative to `factsDir`, or absolute when no relative path exists
43+
* (another drive on Windows), with forward slashes either way.
44+
*/
45+
export function toFactsPath(
46+
factsDir: string,
47+
target: string,
48+
pathApi: path.PlatformPath = path,
49+
): string {
50+
const relative = pathApi.relative(factsDir, target);
51+
const factsPath = pathApi.isAbsolute(relative)
52+
? pathApi.resolve(target)
53+
: relative;
54+
return factsPath.split(pathApi.sep).join("/") || ".";
4255
}
4356

4457
export function createGradleFacts(

0 commit comments

Comments
 (0)