Skip to content

Commit

Permalink
Merge branch 'master' into TS37
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Dec 4, 2019
2 parents 8d251e5 + a6d3c79 commit d9edd58
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Signum.React/Signum.React.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Signum.Analyzer" Version="2.5.0" />
<PackageReference Include="Signum.TSGenerator" Version="2.1.3" />
<PackageReference Include="Signum.TSGenerator" Version="2.2.2" />
</ItemGroup>

<ItemGroup>
Expand Down
112 changes: 44 additions & 68 deletions Signum.TSGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,20 @@ namespace Signum.TSGenerator
{
public static class Program
{


public static int Main(string[] args)
{
var log = Console.Out;

Stopwatch sw = Stopwatch.StartNew();

string intermediateAssembly = args[0];
string[] references = File.ReadAllLines(args[1]);
string[] content = File.ReadAllLines(args[2]);

var log = Console.Out;

log.WriteLine("Starting SignumTSGenerator");

bool hasPdb = File.Exists(Path.ChangeExtension(intermediateAssembly, ".pdb"));

AssemblyDefinition reactAssembly = AssemblyDefinition.ReadAssembly(intermediateAssembly, new ReaderParameters
{
ReadingMode = ReadingMode.Deferred,
ReadSymbols = hasPdb,
InMemory = true,
SymbolReaderProvider = hasPdb ? new PdbReaderProvider() : null
});


if (AlreadyProcessed(reactAssembly))
{
log.WriteLine("SignumTSGenerator already processed: {0}", intermediateAssembly);
return 0;
}

PreloadingAssemblyResolver resolver = new PreloadingAssemblyResolver(references);

var assemblyReferences = (from r in references
where r.Contains(".Entities")
let reactDirectory = FindReactDirectory(r)
Expand All @@ -57,23 +40,42 @@ where r.Contains(".Entities")

var entitiesAssembly = Path.GetFileNameWithoutExtension(intermediateAssembly).Replace(".React", ".Entities");
var entitiesAssemblyReference = assemblyReferences.GetOrThrow(entitiesAssembly);
var entitiesModule = ModuleDefinition.ReadModule(entitiesAssemblyReference.AssemblyFullPath, new ReaderParameters { AssemblyResolver = resolver });

var currentDir = Directory.GetCurrentDirectory();
var files = content
.Where(file => Path.GetExtension(file) == ".t4s")
.Select(file => Path.Combine(currentDir, file))
.ToList();

var upToDateContent = string.Join("\r\n",
new[] { entitiesAssemblyReference.AssemblyFullPath }
.Concat(files)
.OrderBy(a => a)
.Select(f => File.GetLastWriteTimeUtc(f).ToString("o") + " " + Path.GetFileName(f)));

var signumUpToDatePath = Path.Combine(Path.GetDirectoryName(args[1]), "SignumUpToDate.txt");

if(File.Exists(signumUpToDatePath) && File.ReadAllText(signumUpToDatePath) == upToDateContent)
{
log.WriteLine($"SignumTSGenerator already processed ({sw.ElapsedMilliseconds.ToString()}ms)");
return 0;
}

var entityResolver = new PreloadingAssemblyResolver(references);
var entitiesModule = ModuleDefinition.ReadModule(entitiesAssemblyReference.AssemblyFullPath, new ReaderParameters
{
AssemblyResolver = entityResolver
});

var options = new AssemblyOptions
{
CurrentAssembly = entitiesAssembly,
AssemblyReferences = assemblyReferences,
AllReferences = references.ToDictionary(a => Path.GetFileNameWithoutExtension(a)),
ModuleDefinition = entitiesModule,
Resolver = resolver,
Resolver = entityResolver,
};


var currentDir = Directory.GetCurrentDirectory();
var files = content
.Where(file => Path.GetExtension(file) == ".t4s")
.Select(file => Path.Combine(currentDir, file))
.ToList();

bool hasErrors = false;
foreach (var file in files)
{
Expand All @@ -95,52 +97,26 @@ where r.Contains(".Entities")
catch (Exception ex)
{
hasErrors = true;
log.WriteLine($"Error in {file}");
log.WriteLine($"{file}:error STSG0001:{ex.Message}");
log.WriteLine(ex.Message);
}
}

MarkAsProcessed(reactAssembly, resolver);

reactAssembly.Write(intermediateAssembly, new WriterParameters
if (hasErrors)
{
WriteSymbols = hasPdb,
SymbolWriterProvider = hasPdb ? new PdbWriterProvider() : null
});

log.WriteLine($"SignumTSGenerator finished in {sw.ElapsedMilliseconds.ToString()}ms");

Console.WriteLine();

return hasErrors ? -1 : 0;
}

static bool AlreadyProcessed(AssemblyDefinition assembly)
{
var nameof = typeof(GeneratedCodeAttribute).FullName;
var attr = assembly.CustomAttributes
.Any(a => a.AttributeType.FullName == nameof && ((string)a.ConstructorArguments[0].Value) == "SignumTask");

return attr;
}

static void MarkAsProcessed(AssemblyDefinition assembly, IAssemblyResolver resolver)
{
TypeDefinition generatedCodeAttribute = resolver.Resolve(AssemblyNameReference.Parse(typeof(GeneratedCodeAttribute).Assembly.GetName().Name)).MainModule.GetType(typeof(GeneratedCodeAttribute).FullName);
MethodDefinition constructor = generatedCodeAttribute.Methods.Single(a => a.IsConstructor && a.Parameters.Count == 2);

TypeReference stringType = assembly.MainModule.TypeSystem.String;
assembly.CustomAttributes.Add(new CustomAttribute(assembly.MainModule.ImportReference(constructor))
log.WriteLine($"SignumTSGenerator finished with errors ({sw.ElapsedMilliseconds.ToString()}ms)");
Console.WriteLine();
return 0;
}
else
{
ConstructorArguments =
{
new CustomAttributeArgument(stringType, "SignumTask"),
new CustomAttributeArgument(stringType, typeof(Program).Assembly.GetName().Version.ToString()),
}
});
File.WriteAllText(signumUpToDatePath, upToDateContent);
log.WriteLine($"SignumTSGenerator finished ({sw.ElapsedMilliseconds.ToString()}ms)");
Console.WriteLine();
return 0;
}
}


static string FindReactDirectory(string absoluteFilePath)
{
var prefix = absoluteFilePath;
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion Signum.TSGenerator/Signum.TSGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
<LangVersion>latest</LangVersion>
<Platforms>x64;AnyCPU</Platforms>
<Version>2.1.3</Version>
<Version>2.2.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Signum.TSGenerator/Signum.TSGenerator.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Signum.TSGenerator</id>
<version>2.1.3</version>
<version>2.2.2</version>
<title>TypeScript generator for Signum Framework applications</title>
<authors>Olmo del Corral</authors>
<license type="expression">MIT</license>
Expand Down
2 changes: 1 addition & 1 deletion Signum.TSGenerator/Signum.TSGenerator.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Target Name="GenerateSignumTS">
<WriteLinesToFile File="$(BaseIntermediateOutputPath)SignumReferences.txt" Lines="@(ReferencePath)" Overwrite="true" Encoding="Unicode" />
<WriteLinesToFile File="$(BaseIntermediateOutputPath)SignumContent.txt" Lines="@(Content);@(None)" Overwrite="true" Encoding="Unicode" />
<Exec command="dotnet &quot;$(MSBuildThisFileDirectory)Signum.TSGenerator.dll&quot; &quot;@(IntermediateAssembly)&quot; &quot;$(BaseIntermediateOutputPath)SignumReferences.txt&quot; &quot;$(BaseIntermediateOutputPath)SignumContent.txt&quot;" ConsoleToMSBuild="false">
<Exec command="dotnet &quot;$(MSBuildThisFileDirectory)Signum.TSGenerator.dll&quot; &quot;@(IntermediateAssembly)&quot; &quot;$(BaseIntermediateOutputPath)SignumReferences.txt&quot; &quot;$(BaseIntermediateOutputPath)SignumContent.txt&quot;" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
</Exec>
</Target>
Expand Down

2 comments on commit d9edd58

@olmobrutall
Copy link
Collaborator Author

@olmobrutall olmobrutall commented on d9edd58 Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to Typescript 3.7

Typescript 3.7 was released a few weeks ago, but a coloring issue with ?? operator in TSX files in Visual Studio was preventing the merge.

With the new version of Visual Studio (16.4.0) the problem is solved, so no reason not to merge the branch now.

The main benefit is the introduction of ?? and ?. operators in Typescript, both well known from C#.

How to update

  • Update your React.csproj from <TypeScriptToolsVersion>3.6</TypeScriptToolsVersion> to <TypeScriptToolsVersion>3.7</TypeScriptToolsVersion>.
  • Update your nuget from Microsoft.TypeScript.MSBuild 3.6.3 -> 3.7.2
  • Update package.json to "ts-loader": "6.2.1" and "typescript": "3.7.3".
  • Manually search for || operator in tsx files and check if the intention was to make a boolean operation (keep) or give a default value for null/undefined (replace by ??)
  • Replace the pattern bla && bla.name by bla?.name. You can use this regex:
FIND:       (\w+) && \1\.(\w+)
REPLACE:  $1?.$2

Here is a diff of the commits before and after TS 3.7

@MehdyKarimpour
Copy link
Contributor

@MehdyKarimpour MehdyKarimpour commented on d9edd58 Dec 5, 2019 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.