Skip to content

cc Ajaxmin Obfuscator #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: cc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions src/Core/Build/Tasks/ScriptCompilerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,13 @@ private CompilerOptions CreateOptions(IEnumerable<ITaskItem> sourceItems, IEnume
options.IncludeResolver = this;

string scriptFilePath = GetScriptFilePath(locale, minimize, includeTests);
string symbolFilePath = GetSymbolFilePath();

outputScriptItem = new TaskItem(scriptFilePath);
TaskItem outputSymbolItem = new TaskItem(symbolFilePath);

options.ScriptFile = new TaskItemOutputStreamSource(outputScriptItem);
options.SymbolFile = new TaskItemOutputStreamSource(outputSymbolItem);

return options;
}
Expand Down Expand Up @@ -307,6 +312,7 @@ private void GenerateDeploymentFile() {
try {
string assemblyFile = Path.GetFileName(_assembly.ItemSpec);
string scriptsFilePath = Path.Combine(OutputPath, Path.ChangeExtension(assemblyFile, "scripts"));
string symbolFilePath = Path.Combine(OutputPath, Path.ChangeExtension(assemblyFile, "symbol"));

Uri scriptsUri = new Uri(Path.GetFullPath(scriptsFilePath), UriKind.Absolute);
IEnumerable<string> scripts =
Expand Down Expand Up @@ -407,6 +413,16 @@ private string GetScriptFilePath(string locale, bool minimize, bool includeTests
return Path.GetFullPath(Path.Combine(OutputPath, Path.ChangeExtension(scriptName, extension)));
}

private string GetSymbolFilePath() {

string scriptName = Path.GetFileName(_assembly.ItemSpec);
if (Directory.Exists(OutputPath) == false) {
Directory.CreateDirectory(OutputPath);
}
string extension = "symbol";
return Path.GetFullPath(Path.Combine(OutputPath, Path.ChangeExtension(scriptName, extension)));
}

private ICollection<IStreamSource> GetSources(IEnumerable<ITaskItem> sourceItems) {
if (sourceItems == null) {
return new TaskItemInputStreamSource[0];
Expand All @@ -428,13 +444,37 @@ private ICollection<IStreamSource> GetSources(IEnumerable<ITaskItem> sourceItems
return sources;
}

public enum FileTypeEnum {
Normal,
Minimized,
Symbol
}

public string GetExtension(FileTypeEnum fileType) {
switch (fileType) {
case FileTypeEnum.Minimized:
return ".min.js";

case FileTypeEnum.Symbol:
return ".symbol";

case FileTypeEnum.Normal:
default:
return ".js";
}
}


private void OnScriptFileGenerated(ITaskItem scriptItem, CompilerOptions options, bool copyReferences) {
Func<string, bool, string> getScriptFile = delegate(string reference, bool minimized) {
string scriptFile = Path.ChangeExtension(reference, minimized ? ".min.js" : ".js");

Func<string, FileTypeEnum, string> getScriptFile = delegate(string reference, FileTypeEnum fileType)
{
var extension = GetExtension(fileType);
string scriptFile = Path.ChangeExtension(reference, extension);

string fileName = Path.GetFileNameWithoutExtension(scriptFile);
if (fileName.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase)) {
fileName = (minimized ? "ss.min" : "ss") + Path.GetExtension(scriptFile);
fileName = (fileType == FileTypeEnum.Minimized ? "ss.min" : "ss") + Path.GetExtension(scriptFile);
scriptFile = Path.Combine(Path.GetDirectoryName(scriptFile), fileName);
}

Expand Down Expand Up @@ -491,17 +531,23 @@ private void OnScriptFileGenerated(ITaskItem scriptItem, CompilerOptions options

if (copyReferences) {
foreach (string reference in options.References) {
string scriptFile = getScriptFile(reference, /* minimized */ false);
string scriptFile = getScriptFile(reference, FileTypeEnum.Normal);
if (scriptFile != null) {
string path = Path.Combine(scriptFolder, CopyReferencesPath, Path.GetFileName(scriptFile));
safeCopyFile(scriptFile, path);
}

string minScriptFile = getScriptFile(reference, /* minimized */ true);
string minScriptFile = getScriptFile(reference, FileTypeEnum.Minimized);
if (minScriptFile != null) {
string path = Path.Combine(scriptFolder, CopyReferencesPath, Path.GetFileName(minScriptFile));
safeCopyFile(minScriptFile, path);
}

string symbolFile = getScriptFile(reference, FileTypeEnum.Symbol);
if (symbolFile != null) {
string path = Path.Combine(scriptFolder, CopyReferencesPath, Path.GetFileName(symbolFile));
safeCopyFile(symbolFile, path);
}
}
}

Expand Down Expand Up @@ -570,5 +616,7 @@ public TaskItemOutputStreamSource(ITaskItem taskItem, string name)
: base(taskItem.ItemSpec, name) {
}
}


}
}
4 changes: 4 additions & 0 deletions src/Core/Compiler/Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
<Compile Include="..\ResXParser.cs">
<Link>ResXParser.cs</Link>
</Compile>
<Compile Include="SymbolFile\AjaxMinRenameFileGenerator.cs" />
<Compile Include="SymbolFile\SymbolFile.cs" />
<Compile Include="SymbolFile\SymbolFileGenerator.cs" />
<Compile Include="IStreamSourceResolver.cs" />
<Compile Include="ScriptReference.cs" />
<Compile Include="ScriptInfo.cs" />
Expand Down Expand Up @@ -178,6 +181,7 @@
<Compile Include="IStreamSource.cs" />
<Compile Include="ScriptModel\Symbols\IntrinsicType.cs" />
<Compile Include="ScriptModel\Symbols\GenericParameterSymbol.cs" />
<Compile Include="SymbolFile\SymbolFileMerger.cs" />
<Compile Include="Utility.cs" />
<Compile Include="CompilerOptions.cs" />
<Compile Include="IErrorHandler.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Core/Compiler/Compiler/MetadataBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ private void BuildType(TypeSymbol typeSymbol, UserTypeNode typeNode) {

if (AttributeNode.FindAttribute(attributes, "ScriptImport") != null) {
ScriptReference dependency = null;
typeSymbol.SetScriptImported();

AttributeNode dependencyAttribute = AttributeNode.FindAttribute(attributes, "ScriptDependency");
if (dependencyAttribute != null) {
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Compiler/CompilerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed class CompilerOptions {
private ICollection<IStreamSource> _sources;
private ICollection<IStreamSource> _resources;
private IStreamSource _scriptFile;
private IStreamSource _symbolFile;
private IStreamSource _docCommentFile;
private IStreamSourceResolver _includeResolver;
private bool _includeTests;
Expand Down Expand Up @@ -148,6 +149,15 @@ public IStreamSource ScriptFile {
}
}

public IStreamSource SymbolFile {
get {
return _symbolFile;
}
set {
_symbolFile = value;
}
}

public ICollection<IStreamSource> Sources {
get {
return _sources;
Expand Down
6 changes: 4 additions & 2 deletions src/Core/Compiler/Generator/ExpressionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,11 @@ private static void GenerateDelegateExpression(ScriptGenerator generator, Member
}
}
else {
writer.Write("ss.bind('");
writer.Write("ss.bind(");
ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
writer.Write(".");
writer.Write(expression.Method.GeneratedName);
writer.Write("', ");
writer.Write(", ");
ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
writer.Write(")");
}
Expand Down
41 changes: 41 additions & 0 deletions src/Core/Compiler/ScriptCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using ScriptSharp.ResourceModel;
using ScriptSharp.ScriptModel;
using ScriptSharp.Validator;
using ScriptSharp.SymbolFile;

namespace ScriptSharp {

Expand Down Expand Up @@ -223,9 +224,47 @@ public bool Compile(CompilerOptions options) {
return false;
}

GenerateSymbol();
if (_hasErrors) {
return false;
}

return true;
}

private void GenerateSymbol() {

Stream outputStream = null;
TextWriter outputWriter = null;

try {
outputStream = _options.SymbolFile.GetStream();
if (outputStream == null) {
((IErrorHandler)this).ReportError("Unable to write to file " + _options.SymbolFile.FullName,
_options.SymbolFile.FullName);
return;
}

outputWriter = new StreamWriter(outputStream);

var sfg = new SymbolFileGenerator(outputWriter);
sfg.Generate(_symbols);

} catch (Exception e) {
Debug.Fail(e.ToString());
} finally {
if (outputWriter != null) {
outputWriter.Flush();
}
if (outputStream != null) {
_options.SymbolFile.CloseStream(outputStream);
}
}



}

private void GenerateScript() {
Stream outputStream = null;
TextWriter outputWriter = null;
Expand All @@ -243,9 +282,11 @@ private void GenerateScript() {
#if DEBUG
if (_options.InternalTestMode) {
if (_testOutput != null) {
outputWriter.WriteLine("/**");
outputWriter.Write(_testOutput);
outputWriter.WriteLine("Script");
outputWriter.WriteLine("================================================================");
outputWriter.Write("**/");
outputWriter.WriteLine();
outputWriter.WriteLine();
}
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Compiler/ScriptModel/Symbols/TypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal abstract class TypeSymbol : Symbol, ISymbolTable {
private bool _ignoreNamespace;
private string _scriptNamespace;
private bool _testType;
private bool _scriptImported;

protected TypeSymbol(SymbolType type, string name, NamespaceSymbol parent)
: base(type, name, parent) {
Expand Down Expand Up @@ -195,6 +196,12 @@ public string ScriptNamespace {
}
}

public bool IsScriptImported {
get {
return _scriptImported;
}
}

public virtual void AddMember(MemberSymbol memberSymbol) {
Debug.Assert(memberSymbol != null);
Debug.Assert(String.IsNullOrEmpty(memberSymbol.Name) == false);
Expand Down Expand Up @@ -294,6 +301,10 @@ public void SetTestType() {
_testType = true;
}

internal void SetScriptImported() {
_scriptImported = true;
}

#region ISymbolTable Members
ICollection ISymbolTable.Symbols {
get {
Expand Down Expand Up @@ -337,5 +348,6 @@ Symbol ISymbolTable.FindSymbol(string name, Symbol context, SymbolFilter filter)
return symbol;
}
#endregion

}
}
Loading