Skip to content

Commit eafce6c

Browse files
authored
Merge pull request #94 from electricimp/develop
Add path normalization to Github reader
2 parents e913d21 + ecf03d8 commit eafce6c

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

Diff for: README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src=docs/logo.png?2 width=180 alt=Builder><br />
22

3-
### Current version: 4.0.0 ###
3+
### Current version: 4.0.1 ###
44

55
![Build Status](https://cse-ci.electricimp.com/app/rest/builds/buildType:(id:Builder_BuildAndTest)/statusIcon)
66

@@ -181,6 +181,10 @@ builder.machine.directivesUseFile = <false|"PATH_TO_FILE">;
181181
182182
const inputFile = "PATH_TO_YOUR_INPUT_FILE";
183183
184+
// Set the directory of the input file as one of search dirs.
185+
// path.resolve() is used to get the absolute path (requires the path nodejs module)
186+
builder.machine.readers.file.inputFileDir = path.resolve(inputFile);
187+
184188
const result = builder.machine.execute(`@include "${inputFile}"`);
185189
console.log(result);
186190
```
@@ -849,7 +853,7 @@ If
849853
then *Builder* makes the following steps to find the include file:
850854
851855
1. Only if the processed file is a local file, the final path to the include file is a concatenation of the path to the processed file and the path in the include. If the file is not found there, moves to the next step.
852-
1. The final path to the include file is a concatenation of the path to the file specified as the `<input_file>` parameter of the `pleasebuild` command and the path in the include. If the file is not found there, moves to the next step.
856+
1. The final path to the include file is a concatenation of the path to the file specified as the `<input_file>` parameter of the `pleasebuild` command and the path in the include. If the file is not found there, moves to the next step. **Note**: If you use *Builder* as a library and want this search step to work, make sure you specify the input file path (`builder.machine.readers.file.inputFileDir` variable) in your source code within the Builder initialization routine. See the [Library Installation section](#library-installation) to learn more.
853857
1. The final path to the include file is a concatenation of the path to the directory from where the `pleasebuild` command has been called and the path in the include. If the file is not found there, *Builder* reports an error.
854858
855859
##### Examples: #####
@@ -1058,6 +1062,8 @@ To reset the cache, use both the `--cache` and the `--clear-cache` options.
10581062
10591063
If a resource should never be cached, it needs to be added to the `exclude-list.builder` file (see the example below). You can use wildcard characters to mask file names.
10601064
1065+
**Note**: Non-identical paths, even if they point to absolutely the same location, are treated as different paths. Eg., if you include `/a/b/1.nut`, it becomes cached, and then you include `/a/b/../b/1.nut`, it will not be found in the cache.
1066+
10611067
#### Wildcard Pattern Matching ####
10621068
10631069
Pattern matching syntax is a similar to that of *.gitignore*. A string is a wildcard pattern if it contains '```?```' or '```*```' characters. Empty strings or strings that starts with '```#```' are ignored.
@@ -1172,6 +1178,8 @@ A typical `dependencies.json` file looks like this:
11721178
]
11731179
```
11741180
1181+
**Note**: Non-identical paths, even if they point to absolutely the same location, are treated as different paths. Eg., if you have saved dependencies for `/a/b/1.nut` and then you include `/a/b/../b/1.nut` - the dependencies will not be applied to this file.
1182+
11751183
## Including JavaScript Libraries ##
11761184
11771185
Builder can accept JavaScript libraries to add functionality to its global namespace. The library should export an object, the properties of which will be merged into the global namespace. For example, to include a function, *upper()*, to convert strings to uppercase, define your library file like so:

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Builder",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Builder Language Implementation",
55
"main": "src/index.js",
66
"bin": {

Diff for: spec/GithubReader.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ describe('GithubReader', () => {
3535

3636
remote = reader.read('github.com/electricimp/Builder/spec/fixtures/sample-1/input.nut');
3737
expect(remote).toEqual(local);
38+
39+
remote = reader.read('github:electricimp/Builder/./spec/../spec/fixtures/sample-1/input.nut@master');
40+
expect(remote).toEqual(local);
41+
42+
remote = reader.read('github.com:electricimp/Builder/./spec/../spec/fixtures/sample-1/input.nut');
43+
expect(remote).toEqual(local);
3844
});
3945

4046
});

Diff for: spec/Machine/cache.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('FileCache', () => {
5252
machine.useCache = true;
5353
machine.execute(`@include '${linkName}'`);
5454
expect(machine.fileCache._findFile(linkName) ? true : false).toEqual(true);
55-
linkName = 'https://raw.githubusercontent.com/nobitlost/Builder/develop/spec/Builder.spec.js';
55+
linkName = 'https://raw.githubusercontent.com/electricimp/Builder/develop/spec/Builder.spec.js';
5656
machine.execute(`@include '${linkName}'`);
5757
expect(machine.fileCache._findFile(linkName) ? true : false).toEqual(true);
5858
});
@@ -64,7 +64,7 @@ describe('FileCache', () => {
6464
machine.excludeList = __dirname + '/../fixtures/config/exclude-all.exclude';
6565
machine.execute(`@include '${linkName}'`);
6666
expect(machine.fileCache._findFile(linkName)).toEqual(false);
67-
linkName = 'https://raw.githubusercontent.com/nobitlost/Builder/develop/spec/Builder.spec.js';
67+
linkName = 'https://raw.githubusercontent.com/electricimp/Builder/develop/spec/Builder.spec.js';
6868
machine.execute(`@include '${linkName}'`);
6969
expect(machine.fileCache._findFile(linkName)).toEqual(false);
7070
});
@@ -78,10 +78,10 @@ describe('FileCache', () => {
7878
'github:electricimp/Builder/spec/fixtures/sample-11/OneLineSample.nut@v1.0.1',
7979
'github:electricimp/MessageManager/MessageManager.lib.nut',
8080
'github:electricimp/MessageManager/MessageManager.lib.js',
81-
'https://raw.githubusercontent.com/nobitlost/Builder/v2.0.0/src/AstParser.js',
82-
'https://raw.githubusercontent.com/nobitlost/Builder/v2.0.0/src/AstParser.js?nut=2',
83-
'http://raw.githubusercontent.com/nobitlost/Builder/src/AstParser.js',
84-
'http://raw.githubusercontent.com/nobitlost/Builder/src/AstParser.nut'
81+
'https://raw.githubusercontent.com/electricimp/Builder/v2.0.0/src/AstParser.js',
82+
'https://raw.githubusercontent.com/electricimp/Builder/v2.0.0/src/AstParser.js?nut=2',
83+
'http://raw.githubusercontent.com/electricimp/Builder/src/AstParser.js',
84+
'http://raw.githubusercontent.com/electricimp/Builder/src/AstParser.nut'
8585
];
8686
const testFilesList = [
8787
'exclude-all.exclude',

Diff for: spec/Machine/use-dependencies.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Machine', () => {
2929
const rev1Content = "// included file a\n// included file b\n\n\n // should be included\n\n // l2 else\n\n\n // should be included\n";
3030
const rev0GitBlobID = "9db26aa9017943a7812ab6751a699cd1c7247370";
3131
const rev0Content = "// included file a\n // included file b\n\n\n // should be included\n\n // l2 else\n\n\n // should be included\n";
32-
const url = 'github:nobitlost/Builder/spec/fixtures/sample-1/input.nut.out';
32+
const url = 'github:electricimp/Builder/spec/fixtures/sample-1/input.nut.out';
3333

3434
// ensure that test dependencies JSON file does not exist
3535
if (fs.existsSync(dependenciesSaveFile)) {
@@ -64,7 +64,7 @@ describe('Machine', () => {
6464
const rev0GitBlobID = "9db26aa9017943a7812ab6751a699cd1c7247370";
6565
const rev0CommitID = "e2a5b434b34b5737b2ff52f51a92c5bbcc9f83bf";
6666
const rev0Content = "// included file a\n // included file b\n\n\n // should be included\n\n // l2 else\n\n\n // should be included\n";
67-
const url = `github:nobitlost/Builder/spec/fixtures/sample-1/input.nut.out@${rev0CommitID}`;
67+
const url = `github:electricimp/Builder/spec/fixtures/sample-1/input.nut.out@${rev0CommitID}`;
6868

6969
// ensure that test dependencies JSON file does not exist
7070
if (fs.existsSync(dependenciesSaveFile)) {
@@ -85,7 +85,7 @@ describe('Machine', () => {
8585

8686
it('Check ---save-dependecies/--use-dependencies options combination', () => {
8787
const rev1Content = "// included file a\n// included file b\n\n\n // should be included\n\n // l2 else\n\n\n // should be included\n";
88-
const url = `github:nobitlost/Builder/spec/fixtures/sample-1/input.nut.out`;
88+
const url = `github:electricimp/Builder/spec/fixtures/sample-1/input.nut.out`;
8989

9090
// ensure that test dependencies JSON file does not exist
9191
if (fs.existsSync(dependenciesUseFile)) {
@@ -119,7 +119,7 @@ describe('Machine', () => {
119119

120120
it('Check case when dependencies JSON file is corrupted', () => {
121121
const rev1Content = "// included file a\n// included file b\n\n\n // should be included\n\n // l2 else\n\n\n // should be included\n";
122-
const url = `github:nobitlost/Builder/spec/fixtures/sample-1/input.nut.out`;
122+
const url = `github:electricimp/Builder/spec/fixtures/sample-1/input.nut.out`;
123123

124124
// ensure that test dependencies JSON file does not exist
125125
if (fs.existsSync(dependenciesSaveFile)) {
@@ -136,7 +136,7 @@ describe('Machine', () => {
136136
// corrupt the file
137137
fs.appendFileSync(dependenciesSaveFile, ']');
138138

139-
const fileCorruptedMessage = `The dependencies JSON file '${dependenciesSaveFile}' cannot be used: Unexpected token ] in JSON at position 127`;
139+
const fileCorruptedMessage = `The dependencies JSON file '${dependenciesSaveFile}' cannot be used: Unexpected token ] in JSON at position 129`;
140140

141141
// check exception error message
142142
try {

Diff for: src/Readers/GithubReader.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class GithubReader extends AbstractReader {
6666

6767
// process dependencies
6868
if (options && options.dependencies && options.dependencies.has(source)) {
69-
this.gitBlobID = options.dependencies.get(source);
69+
var gitBlobID = options.dependencies.get(source);
7070
}
7171

7272
// spawn child process
@@ -77,7 +77,7 @@ class GithubReader extends AbstractReader {
7777
source,
7878
this.username,
7979
this.token,
80-
this.gitBlobID,
80+
gitBlobID,
8181
],
8282
{ timeout: this.timeout }
8383
);
@@ -198,10 +198,13 @@ class GithubReader extends AbstractReader {
198198

199199
const octokit = new Octokit(octokitConfig);
200200

201+
const parsedUrl = this.parseUrl(source);
202+
parsedUrl.path = upath.normalize(parsedUrl.path);
203+
201204
if (gitBlobID !== 'undefined') {
202205
const args = {
203-
owner: this.parseUrl(source).owner,
204-
repo: this.parseUrl(source).repo,
206+
owner: parsedUrl.owner,
207+
repo: parsedUrl.repo,
205208
file_sha: gitBlobID,
206209
};
207210

@@ -220,7 +223,7 @@ class GithubReader extends AbstractReader {
220223
}
221224

222225
// @see https://developer.github.com/v3/repos/contents/#get-contents
223-
octokit.repos.getContents(this.parseUrl(source))
226+
octokit.repos.getContents(parsedUrl)
224227
.then((res) => {
225228
const ret = {
226229
data: Buffer.from(res.data.content, 'base64').toString(),

0 commit comments

Comments
 (0)