示例#1
0
        static GeneratedSolcData GetSolcCompilationData(AppOptions opts)
        {
            const string GENERATED_NAMESPACE = "Meadow.DebugSol.Generated";

            // Setup codegen/compilation options.
            var solCodeGenArgs = new CommandArgs
            {
                Generate                = GenerateOutputType.Source | GenerateOutputType.Assembly,
                Namespace               = GENERATED_NAMESPACE,
                OutputDirectory         = opts.SourceOutputDir,
                AssemblyOutputDirectory = opts.BuildOutputDir,
                SolSourceDirectory      = opts.SolCompilationSourcePath
            };

            // Perform sol compilation if out-of-date.
            var solCodeGenResults = CodebaseGenerator.Generate(solCodeGenArgs, logger: msg => { });

            // Load compiled assembly.
            var generatedAsm = AppDomain.CurrentDomain.Load(
                File.ReadAllBytes(solCodeGenResults.CompilationResults.AssemblyFilePath),
                File.ReadAllBytes(solCodeGenResults.CompilationResults.PdbFilePath));

            // Load solc data from assembly resources.
            return(GeneratedSolcData.Create(generatedAsm));
        }
示例#2
0
        public void TestCompiledAssembly()
        {
            var solCodeGenArgs = new CommandArgs
            {
                OutputDirectory         = _outputDir,
                AssemblyOutputDirectory = _assemblyOutputDir,
                SolSourceDirectory      = _sourceDir,
                Generate  = GenerateOutputType.Source | GenerateOutputType.Assembly,
                Namespace = _namespace
            };

            var solCodeGenResults = CodebaseGenerator.Generate(solCodeGenArgs);

            Assembly loadedAssembly;
            var      assemblyBytes = File.ReadAllBytes(solCodeGenResults.CompilationResults.AssemblyFilePath);
            var      pdbBytes      = File.ReadAllBytes(solCodeGenResults.CompilationResults.PdbFilePath);

            loadedAssembly = Assembly.Load(assemblyBytes, pdbBytes);

            var solcDataParser = GeneratedSolcData.Create(loadedAssembly);
            var data           = solcDataParser.GetSolcData();
        }
示例#3
0
        // TODO: This requires the ability to unload/reload an assembly which is not yet avaiable in dotnetcore.
        //       Investigate compiling Contract classes with GUID, then use powershell alias for easy access,
        //       and re-defining aliases during new contract class assembly loading.

        public static void Run(SessionState sessionState)
        {
            var guid              = Util.GetUniqueID();
            var tempDir           = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), guid)).FullName;
            var sourceOutputDir   = Path.Combine(tempDir, "GeneratedOutput");
            var assemblyOutputDir = Path.Combine(tempDir, "GeneratedAssembly");
            var @namespace        = "Gen" + guid + ".Contracts";

            var    config       = Config.Read(sessionState.Path.CurrentLocation.Path);
            string solSourceDir = Util.GetSolSourcePath(config, sessionState);

            Version solcVersion = null;

            if (!string.IsNullOrWhiteSpace(config.SolcVersion) && !config.SolcVersion.Trim().Equals("latest", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    solcVersion = Version.Parse(config.SolcVersion);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine($"Could not parse solc version value '{config.SolcVersion}'");
                    Console.Error.WriteLine(ex);
                    return;
                }
            }

            var solCodeGenResults = CodebaseGenerator.Generate(new CommandArgs
            {
                Generate                = GenerateOutputType.Source | GenerateOutputType.Assembly,
                Namespace               = @namespace,
                OutputDirectory         = sourceOutputDir,
                AssemblyOutputDirectory = assemblyOutputDir,
                SolSourceDirectory      = solSourceDir,
                SolcOptimizer           = (int)config.SolcOptimizer,
                SolcVersion             = solcVersion
            });

            var assemblyBytes        = File.ReadAllBytes(solCodeGenResults.CompilationResults.AssemblyFilePath);
            var pdbBytes             = File.ReadAllBytes(solCodeGenResults.CompilationResults.PdbFilePath);
            var loadedAssembly       = Assembly.Load(assemblyBytes, pdbBytes);
            var referencedAssemblies = Compilation.GetReferencedAssemblies(loadedAssembly)
                                       .Select(a => a.Value.Location)
                                       .Where(p => !string.IsNullOrEmpty(p))
                                       .ToList();

#if CAN_RUN_CMD
            var result = PowerShell.Create(RunspaceMode.CurrentRunspace)
                         .AddCommand("Add-Type")
                         .AddParameter("-Path", solCodeGenResults.CompilationResults.AssemblyFilePath)
                         .AddParameter("-ReferencedAssemblies", referencedAssemblies.ToArray())
                         .Invoke();
#endif

            var contractTypes = loadedAssembly.ExportedTypes
                                .Where(t => t.BaseType == typeof(BaseContract))
                                .ToArray();

            GlobalVariables.SetContractTypes(sessionState, contractTypes);

            // TODO: programmatic generation of format files for Contract and Event types, then load with Update-FormatData

            dynamic contractHolder = new ContractTypeHolder(contractTypes);
            sessionState.PSVariable.Set(new PSVariable(GlobalVariables.VAR_NAMES.CONTRACTS, contractHolder, ScopedItemOptions.AllScope));
        }