Skip to content

Commit 8d92dc2

Browse files
committed
initial playings.. very brute force
1 parent 7121660 commit 8d92dc2

28 files changed

+439
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ node_modules
2626

2727
# Users Environment Variables
2828
.lock-wscript
29+
30+
#visual studio files
31+
*.suo
32+
*.user
33+
*.sln.docstates
34+
.ntvs_analysis.dat
35+
36+
#tsd
37+
typings/

gulp-typescript-order.njsproj

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<SchemaVersion>2.0</SchemaVersion>
6+
<ProjectGuid>{b856821d-594a-458d-aea0-5e4e6a236af9}</ProjectGuid>
7+
<ProjectHome />
8+
<ProjectView>ProjectFiles</ProjectView>
9+
<StartupFile>index.js</StartupFile>
10+
<WorkingDirectory>.</WorkingDirectory>
11+
<OutputPath>.</OutputPath>
12+
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{349c5851-65df-11da-9384-00065b846f21};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
13+
<TypeScriptSourceMap>true</TypeScriptSourceMap>
14+
<TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
15+
<EnableTypeScript>true</EnableTypeScript>
16+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
17+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
18+
</PropertyGroup>
19+
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
20+
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
21+
<ItemGroup>
22+
<Content Include="package.json" />
23+
<Content Include="README.md" />
24+
<Compile Include="gulpfile.js" />
25+
<Compile Include="helpers\file-lister.js" />
26+
<Compile Include="index.js" />
27+
</ItemGroup>
28+
<ItemGroup>
29+
<Folder Include="helpers\" />
30+
<Folder Include="sample\" />
31+
<Folder Include="typings\" />
32+
<Folder Include="typings\angularjs\" />
33+
</ItemGroup>
34+
<ItemGroup>
35+
<TypeScriptCompile Include="sample\aaconfig.ts" />
36+
<TypeScriptCompile Include="sample\angulardef.ts" />
37+
<TypeScriptCompile Include="sample\test1.ts" />
38+
<TypeScriptCompile Include="sample\test2.ts" />
39+
<TypeScriptCompile Include="sample\test3.ts" />
40+
<TypeScriptCompile Include="sample\zz.ts" />
41+
<TypeScriptCompile Include="typings\angularjs\angular.d.ts" />
42+
<TypeScriptCompile Include="typings\tsd.d.ts" />
43+
</ItemGroup>
44+
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45+
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
46+
<ProjectExtensions>
47+
<VisualStudio>
48+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
49+
<WebProjectProperties>
50+
<UseIIS>False</UseIIS>
51+
<AutoAssignPort>True</AutoAssignPort>
52+
<DevelopmentServerPort>0</DevelopmentServerPort>
53+
<DevelopmentServerVPath>/</DevelopmentServerVPath>
54+
<IISUrl>http://localhost:48022/</IISUrl>
55+
<NTLMAuthentication>False</NTLMAuthentication>
56+
<UseCustomServer>True</UseCustomServer>
57+
<CustomServerUrl>http://localhost:1337</CustomServerUrl>
58+
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
59+
</WebProjectProperties>
60+
</FlavorProperties>
61+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}" User="">
62+
<WebProjectProperties>
63+
<StartPageUrl>
64+
</StartPageUrl>
65+
<StartAction>CurrentPage</StartAction>
66+
<AspNetDebugging>True</AspNetDebugging>
67+
<SilverlightDebugging>False</SilverlightDebugging>
68+
<NativeDebugging>False</NativeDebugging>
69+
<SQLDebugging>False</SQLDebugging>
70+
<ExternalProgram>
71+
</ExternalProgram>
72+
<StartExternalURL>
73+
</StartExternalURL>
74+
<StartCmdLineArguments>
75+
</StartCmdLineArguments>
76+
<StartWorkingDirectory>
77+
</StartWorkingDirectory>
78+
<EnableENC>False</EnableENC>
79+
<AlwaysStartWebServerOnDebug>False</AlwaysStartWebServerOnDebug>
80+
</WebProjectProperties>
81+
</FlavorProperties>
82+
</VisualStudio>
83+
</ProjectExtensions>
84+
</Project>

gulp-typescript-order.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.31101.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "gulp-typescript-order", "gulp-typescript-order.njsproj", "{B856821D-594A-458D-AEA0-5E4E6A236AF9}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B856821D-594A-458D-AEA0-5E4E6A236AF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B856821D-594A-458D-AEA0-5E4E6A236AF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B856821D-594A-458D-AEA0-5E4E6A236AF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B856821D-594A-458D-AEA0-5E4E6A236AF9}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

gulpfile.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var gulp = require('gulp');
2+
var tsOrder = require('./index');
3+
var angularSort = require('gulp-angular-filesort');
4+
var ts = require('gulp-typescript');
5+
var lister = require('./helpers/file-lister');
6+
7+
var tsProject = ts.createProject({
8+
sortOutput: false,
9+
declarationFiles: false,
10+
target: 'es5',
11+
sourceRoot: '../',
12+
noExternalResolve: false
13+
});
14+
15+
gulp.task('order', function (){
16+
return gulp.src("sample/*.ts")
17+
.pipe(tsOrder(true));
18+
})
19+
20+
gulp.task('default', function () {
21+
var tsStream = gulp.src('sample/*.ts')
22+
//.pipe(lister())
23+
.pipe(tsOrder())
24+
.pipe(lister('after ts-order'))
25+
.pipe(ts(tsProject));
26+
27+
return tsStream.js
28+
.pipe(lister('after tsc'))
29+
.pipe(angularSort())
30+
.pipe(lister('after ng-sort'))
31+
;
32+
});

helpers/file-lister.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var es = require('event-stream');
2+
3+
module.exports = function lister(name) {
4+
5+
var isFirst = true;
6+
7+
return es.through(
8+
function firstPass(file) {
9+
10+
if (isFirst && name) {
11+
console.log(name);
12+
console.log('-----');
13+
}
14+
15+
isFirst = false;
16+
var relPath = file.path.substring(file.base.length);
17+
18+
console.log(relPath);
19+
this.emit('data', file);
20+
21+
22+
23+
},
24+
function secondPass() {
25+
console.log('-----\n');
26+
this.emit('end');
27+
28+
});
29+
30+
}

index.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
var es = require('event-stream');
2+
var toposort = require('toposort');
3+
var gutil = require('gulp-util');
4+
var ts = require('typescript-services');
5+
6+
module.exports = function typescriptSort(debug) {
7+
8+
var classes = {};
9+
var references = [];
10+
var files = [];
11+
12+
function parse(text) {
13+
14+
var re = /class\s+(\w+)\s*(extends\s+([a-z0-9-.]+)\s*)?[{|$]/ig
15+
var match = null;
16+
17+
var found = [];
18+
19+
while ((match = re.exec(text)) != null) {
20+
21+
found.push({
22+
className: match[1],
23+
baseClassName: match[3],
24+
module: /module\s+([a-z0-9-]+)/i.exec(text)[1]
25+
});
26+
}
27+
28+
var angRe = /\.(controller|service|factory|provider|directive)\([^,]+?,\s*([^)]+)\)/g;
29+
while ((match = angRe.exec(text)) != null) {
30+
31+
found.push({
32+
baseClassName: match[2]
33+
});
34+
}
35+
36+
return found;
37+
}
38+
39+
// console.log();
40+
41+
return es.through(
42+
function firstPass(file) {
43+
44+
var found = parse(file.contents.toString());
45+
46+
var relPath = file.path.substring(file.base.length);
47+
file.relPath = relPath;
48+
49+
for (var i = 0; i < found.length; i++) {
50+
var classDef = found[i];
51+
52+
//console.log(classDef.className + ' -> ' + classDef.baseClassName);
53+
54+
classDef.relPath = relPath;
55+
classDef.path = file.path;
56+
classDef.file = file;
57+
58+
if (classDef.className) {
59+
var fullClassName = classDef.module + '.' + classDef.className;
60+
classes[fullClassName] = classDef;
61+
62+
} else {
63+
references.push(classDef)
64+
}
65+
}
66+
67+
//console.log(file.relPath);
68+
files.push(file);
69+
//this.emit('data', file);
70+
},
71+
function secondPass() {
72+
73+
//console.log();
74+
75+
var toSort = [];
76+
77+
for (var key in classes) {
78+
var def = classes[key];
79+
80+
if (!def.baseClassName) {
81+
//toSort.push([def.file, null]);
82+
continue;
83+
}
84+
// continue;
85+
86+
var depModule = classes[def.baseClassName] ||
87+
classes[def.module + '.' + def.baseClassName];
88+
89+
if (!depModule)
90+
continue;
91+
92+
if(debug)
93+
console.log(def.file.relPath + ' -> ' + depModule.file.relPath);
94+
95+
toSort.push([def.file, depModule.file]);
96+
97+
}
98+
//console.log();
99+
100+
for (var i = 0; i < references.length; i++) {
101+
var def = references[i];
102+
103+
//if (debug) console.log('ref: ' + def.baseClassName);
104+
var depClass = classes[def.baseClassName];
105+
if (depClass) {
106+
//if (debug) console.log(' :' + depClass.file.relPath);
107+
toSort.push([def.file, depClass.file]);
108+
109+
if (debug)
110+
console.log(def.file.relPath + ' :> ' + depClass.file.relPath);
111+
}
112+
}
113+
114+
if (debug) console.log();
115+
116+
var self = this;
117+
toposort.array(files, toSort)
118+
.reverse()
119+
.forEach(function (file, g) {
120+
self.emit('data', file);
121+
// console.log(g);
122+
if(debug)
123+
console.log(file.relPath);
124+
});
125+
126+
this.emit('end');
127+
});
128+
129+
}

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "gulp-typescript-order",
3+
"version": "0.1.0",
4+
"description": "Gulp plugin to re-order Typescript files based on their dependencies",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/Xiot/gulp-typescript-order.git"
12+
},
13+
"keywords": [
14+
"gulp",
15+
"typescript"
16+
],
17+
"author": "Chris Thomas",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/Xiot/gulp-typescript-order/issues"
21+
},
22+
"homepage": "https://github.com/Xiot/gulp-typescript-order",
23+
"dependencies": {
24+
"event-stream": "^3.1.7",
25+
"gulp-util": "^3.0.1",
26+
"toposort": "^0.2.10",
27+
"toposort-extended": "^0.1.2",
28+
"typescript": "^1.3.0",
29+
"typescript-services": "^0.1.5"
30+
},
31+
"devDependencies": {
32+
"gulp": "^3.8.10",
33+
"gulp-typescript": "^2.3.0"
34+
}
35+
}

sample/aaconfig.js

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

sample/aaconfig.js.map

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

sample/aaconfig.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
angular.module('def')
2+
.controller('d', ts.Test2);

sample/angular-def.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//# sourceMappingURL=angular-def.js.map

sample/angular-def.js.map

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

sample/angulardef.js

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

sample/angulardef.js.map

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

sample/angulardef.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <reference path="../typings/tsd.d.ts" />
2+
3+
angular.module('def', [])
4+
.controller('test1', ts.Test1);

0 commit comments

Comments
 (0)