Skip to content

Commit 83ea676

Browse files
Merge pull request #105 from DustinCampbell/gulp-cleanup
Factor code to download OmniSharp out of gulpfile.js
2 parents ea88cb0 + 254bc70 commit 83ea676

File tree

9 files changed

+503
-89
lines changed

9 files changed

+503
-89
lines changed

gulpfile.js

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,35 @@
1-
var gulp = require('gulp');
2-
var decompress = require('gulp-decompress');
3-
var tslint = require("gulp-tslint");
4-
var es = require('event-stream');
5-
var GitHub = require('github-releases');
6-
var tmp = require('tmp');
7-
var vfs = require('vinyl-fs');
8-
var del = require('del');
9-
var fs = require('fs');
10-
var path = require('path');
11-
12-
tmp.setGracefulCleanup();
13-
14-
function downloadOmnisharp(version) {
15-
var result = es.through();
16-
17-
function onError(err) {
18-
result.emit('error', err);
19-
}
20-
21-
var repo = new GitHub({
22-
repo: 'OmniSharp/omnisharp-roslyn',
23-
token: process.env['GITHUB_TOKEN']
24-
});
25-
26-
repo.getReleases({ tag_name: version }, function (err, releases) {
27-
if (err) { return onError(err); }
28-
if (!releases.length) { return onError(new Error('Release not found')); }
29-
if (!releases[0].assets.length) { return onError(new Error('Assets not found')); }
30-
31-
repo.downloadAsset(releases[0].assets[0], function (err, istream) {
32-
if (err) { return onError(err); }
33-
34-
tmp.file(function (err, tmpPath, fd, cleanupCallback) {
35-
if (err) { return onError(err); }
36-
37-
var ostream = fs.createWriteStream(null, { fd: fd });
38-
ostream.once('error', onError);
39-
istream.once('error', onError);
40-
ostream.once('finish', function () {
41-
vfs.src(tmpPath).pipe(result);
42-
});
43-
istream.pipe(ostream);
44-
});
45-
});
46-
});
47-
48-
return result;
49-
}
50-
51-
gulp.task('omnisharp:clean', function () {
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
const decompress = require('gulp-decompress');
9+
const del = require('del');
10+
const fs = require('fs-extra-promise');
11+
const gulp = require('gulp');
12+
const path = require('path');
13+
const tslint = require('gulp-tslint');
14+
const omnisharpInstall = require('./out/omnisharpInstall');
15+
16+
gulp.task('omnisharp:clean', () => {
5217
return del('bin');
5318
});
5419

55-
gulp.task('omnisharp:fetch', ['omnisharp:clean'], function () {
56-
return downloadOmnisharp('v1.6.7.9')
20+
gulp.task('omnisharp:fetch', ['omnisharp:clean'], () => {
21+
return omnisharpInstall.downloadOmnisharp('v1.6.7.9')
5722
.pipe(decompress({strip: 1}))
5823
.pipe(gulp.dest('bin'));
5924
});
6025

61-
var allTypeScript = [
26+
const allTypeScript = [
6227
'src/**/*.ts',
6328
'!**/*.d.ts',
6429
'!**/typings**'
6530
];
6631

67-
var lintReporter = function (output, file, options) {
32+
const lintReporter = (output, file, options) => {
6833
//emits: src/helloWorld.c:5:3: warning: implicit declaration of function ‘prinft’
6934
var relativeBase = file.base.substring(file.cwd.length + 1).replace('\\', '/');
7035
output.forEach(function(e) {
@@ -73,7 +38,7 @@ var lintReporter = function (output, file, options) {
7338
});
7439
};
7540

76-
gulp.task('tslint', function () {
41+
gulp.task('tslint', () => {
7742
gulp.src(allTypeScript)
7843
.pipe(tslint({
7944
rulesDirectory: "node_modules/tslint-microsoft-contrib"
@@ -84,37 +49,37 @@ gulp.task('tslint', function () {
8449
}))
8550
});
8651

87-
gulp.task('omnisharp:fixscripts', ['omnisharp:fetch'], function () {
52+
gulp.task('omnisharp:fixscripts', ['omnisharp:fetch'], () => {
8853

8954
var _fixes = Object.create(null);
9055
_fixes['./bin/omnisharp.cmd'] = '@"%~dp0packages\\dnx-clr-win-x86.1.0.0-beta4\\bin\\dnx.exe" "%~dp0packages\\OmniSharp\\1.0.0\\root" run %*';
91-
_fixes['./bin/omnisharp'] = '#!/bin/bash\n\
92-
SOURCE="${BASH_SOURCE[0]}"\n\
93-
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink\n\
94-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"\n\
95-
SOURCE="$(readlink "$SOURCE")"\n\
96-
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located\n\
97-
done\n\
98-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"\n\
99-
export SET DNX_APPBASE="$DIR/packages/OmniSharp/1.0.0/root"\n\
100-
export PATH=/usr/local/bin:/Library/Frameworks/Mono.framework/Commands:$PATH # this is required for the users of the Homebrew Mono package\n\
101-
exec "$DIR/packages/dnx-mono.1.0.0-beta4/bin/dnx" "$DNX_APPBASE" run "$@"\n\
102-
\n';
103-
104-
var promises = Object.keys(_fixes).map(function (key) {
105-
return new Promise(function(resolve, reject) {
106-
fs.writeFile(path.join(__dirname, key), _fixes[key], function (err) {
56+
_fixes['./bin/omnisharp'] = '#!/bin/bash\n'
57+
+ 'SOURCE="${BASH_SOURCE[0]}"\n'
58+
+ 'while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink\n'
59+
+ 'DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"\n'
60+
+ 'SOURCE="$(readlink "$SOURCE")"\n'
61+
+ '[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located\n'
62+
+ 'done\n'
63+
+ 'DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"\n'
64+
+ 'export SET DNX_APPBASE="$DIR/packages/OmniSharp/1.0.0/root"\n'
65+
+ 'export PATH=/usr/local/bin:/Library/Frameworks/Mono.framework/Commands:$PATH # this is required for the users of the Homebrew Mono package\n'
66+
+ 'exec "$DIR/packages/dnx-mono.1.0.0-beta4/bin/dnx" "$DNX_APPBASE" run "$@"\n'
67+
+ '\n';
68+
69+
const promises = Object.keys(_fixes).map(key => {
70+
return new Promise((resolve, reject) => {
71+
fs.writeFile(path.join(__dirname, key), _fixes[key], err => {
10772
if (err) {
10873
reject(err);
109-
} else {
74+
}
75+
else {
11076
resolve();
11177
}
112-
})
78+
});
11379
});
11480
});
11581

11682
return Promise.all(promises)
11783
});
11884

119-
12085
gulp.task('omnisharp', ['omnisharp:fixscripts']);

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@
1515
],
1616
"main": "./out/omnisharpMain",
1717
"scripts": {
18-
"postinstall": "gulp omnisharp && tsc"
18+
"postinstall": "tsc && gulp omnisharp"
1919
},
2020
"dependencies": {
21+
"del": "^2.0.2",
22+
"event-stream": "^3.3.2",
2123
"fs-extra-promise": "^0.3.1",
24+
"github-releases": "^0.3.0",
2225
"run-in-terminal": "*",
2326
"semver": "*",
24-
"vscode-extension-telemetry": "0.0.4"
27+
"vscode-extension-telemetry": "0.0.4",
28+
"tmp": "0.0.28",
29+
"vinyl-fs": "^2.2.1"
2530
},
2631
"devDependencies": {
27-
"del": "^2.0.2",
28-
"event-stream": "^3.3.2",
29-
"github-releases": "^0.3.0",
3032
"gulp": "^3.8.9",
3133
"gulp-decompress": "^1.2.0",
3234
"gulp-tslint": "^4.3.0",
33-
"tmp": "0.0.28",
3435
"tslint": "^3.3.0",
3536
"tslint-microsoft-contrib": "^2.0.0",
3637
"typescript": "^1.7.3",
37-
"vinyl-fs": "^2.2.1",
3838
"vscode": "^0.10.1"
3939
},
4040
"engines": {

src/omnisharpInstall.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
import * as fs from 'fs-extra-promise';
9+
import * as tmp from 'tmp';
10+
import * as vfs from 'vinyl-fs';
11+
12+
const es = require('event-stream');
13+
const github = require('github-releases');
14+
15+
tmp.setGracefulCleanup();
16+
17+
export function downloadOmnisharp(version: string) {
18+
var result = es.through();
19+
20+
function onError(err) {
21+
result.emit('error', err);
22+
}
23+
24+
var repo = new github({
25+
repo: 'OmniSharp/omnisharp-roslyn',
26+
token: process.env['GITHUB_TOKEN']
27+
});
28+
29+
repo.getReleases({ tag_name: version }, function (err, releases) {
30+
if (err) {
31+
return onError(err);
32+
}
33+
34+
if (!releases.length) {
35+
return onError(new Error('Release not found'));
36+
}
37+
38+
if (!releases[0].assets.length) {
39+
return onError(new Error('Assets not found'));
40+
}
41+
42+
repo.downloadAsset(releases[0].assets[0], function (err, istream) {
43+
if (err) {
44+
return onError(err);
45+
}
46+
47+
tmp.file(function (err, tmpPath, fd, cleanupCallback) {
48+
if (err) {
49+
return onError(err);
50+
}
51+
52+
var ostream = fs.createWriteStream(null, { fd: fd });
53+
ostream.once('error', onError);
54+
istream.once('error', onError);
55+
ostream.once('finish', function () {
56+
vfs.src(tmpPath).pipe(result);
57+
});
58+
59+
istream.pipe(ostream);
60+
});
61+
});
62+
});
63+
64+
return result;
65+
}

src/typings/del/del.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Type definitions for del v2.2.0
2+
// Project: https://github.com/sindresorhus/del
3+
// Definitions by: Asana <https://asana.com>, Aya Morisawa <https://github.com/AyaMorisawa>
4+
5+
// /// <reference path="../glob/glob.d.ts"/>
6+
// /// <reference path="../es6-promise/es6-promise.d.ts" />
7+
8+
declare module "del" {
9+
import glob = require("glob");
10+
11+
function Del(pattern: string): Promise<string[]>;
12+
function Del(pattern: string, options: Del.Options): Promise<string[]>;
13+
14+
function Del(patterns: string[]): Promise<string[]>;
15+
function Del(patterns: string[], options: Del.Options): Promise<string[]>;
16+
17+
module Del {
18+
function sync(pattern: string, options?: Options): string[];
19+
function sync(patterns: string[], options?: Options): string[];
20+
21+
interface Options extends glob.IOptions {
22+
force?: boolean;
23+
dryRun?: boolean;
24+
}
25+
}
26+
27+
export = Del;
28+
}

src/typings/fs-extra-promise/fs-extra-promise.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
// Imported from: https://github.com/soywiz/typescript-node-definitions/fs-extra.d.ts via TSD fs-extra definition
66

7-
// ///<reference path="../node/node.d.ts"/>
8-
// ///<reference path="../bluebird/bluebird.d.ts"/>
7+
///<reference path="../bluebird/bluebird.d.ts"/>
8+
///<reference path="../ref.d.ts"/>
99

1010
declare module "fs-extra-promise" {
1111
import stream = require("stream");
@@ -177,13 +177,16 @@ declare module "fs-extra-promise" {
177177
encoding?: string;
178178
fd?: number;
179179
mode?: number;
180-
bufferSize?: number;
180+
autoClose?: boolean;
181181
}
182+
182183
export interface WriteStreamOptions {
183184
flags?: string;
184-
encoding?: string;
185-
string?: string;
185+
defaultEncounding?: string;
186+
fd?: number;
187+
mode?: number;
186188
}
189+
187190
export function createReadStream(path: string, options?: ReadStreamOptions): ReadStream;
188191
export function createWriteStream(path: string, options?: WriteStreamOptions): WriteStream;
189192

src/typings/ref.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
/// <reference path='../../node_modules/vscode/typings/index.d.ts'/>
6+
/// <reference path='../../node_modules/vscode/typings/index.d.ts'/>
7+
/// <reference path='../../node_modules/vscode/typings/glob.d.ts'/>

src/typings/tmp/tmp.d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Type definitions for tmp v0.0.28
2+
// Project: https://www.npmjs.com/package/tmp
3+
// Definitions by: Jared Klopper <https://github.com/optical>
4+
5+
declare module "tmp" {
6+
7+
module tmp {
8+
interface Options extends SimpleOptions {
9+
mode?: number;
10+
}
11+
12+
interface SimpleOptions {
13+
prefix?: string;
14+
postfix?: string;
15+
template?: string;
16+
dir?: string;
17+
tries?: number;
18+
keep?: boolean;
19+
unsafeCleanup?: boolean;
20+
}
21+
22+
interface SynchrounousResult {
23+
name: string;
24+
fd: number;
25+
removeCallback: () => void;
26+
}
27+
28+
function file(callback: (err: any, path: string, fd: number, cleanupCallback: () => void) => void): void;
29+
function file(config: Options, callback?: (err: any, path: string, fd: number, cleanupCallback: () => void) => void): void;
30+
31+
function fileSync(config?: Options): SynchrounousResult;
32+
33+
function dir(callback: (err: any, path: string, cleanupCallback: () => void) => void): void;
34+
function dir(config: Options, callback?: (err: any, path: string, cleanupCallback: () => void) => void): void;
35+
36+
function dirSync(config?: Options): SynchrounousResult;
37+
38+
function tmpName(callback: (err: any, path: string) => void): void;
39+
function tmpName(config: SimpleOptions, callback?: (err: any, path: string) => void): void;
40+
41+
function tmpNameSync(config?: SimpleOptions): string;
42+
43+
function setGracefulCleanup(): void;
44+
}
45+
46+
export = tmp;
47+
}

0 commit comments

Comments
 (0)