Example #1
0
        public void buildFiles()
        {
            if (Files == null)
            {
                throw new InvalidProgramException("must specify which files to compile");
            }

            var actualFiles = Files.ToArray();

            if (actualFiles.Length == 0)
            {
                throw new InvalidProgramException($"must specify which files to compile");
            }

            var asExe = actualFiles
                        .Where(path => Path
                               .GetFileName(path)
                               .Equals("Program.cs"))
                        .Any();

            if (Extensions == null)
            {
                var exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                Extensions = directoryExtensions(exePath);
            }

            var compilation = new ExcessCompilation(
                analysis: new CompilationAnalysis(),
                extensions: Extensions,
                executable: asExe);

            foreach (var file in actualFiles)
            {
                if (file.EndsWith(".xs.cs"))
                {
                    continue;
                }

                var ext = Path.GetExtension(file);
                switch (ext)
                {
                case ".cs": compilation.addCSharpFile(file); break;

                case ".xs": compilation.addDocument(file); break;

                default: throw new InvalidOperationException($"invalid extension: {ext}");
                }
            }

            var errors = null as IEnumerable <Diagnostic>;
            var result = compilation.build(out errors);

            if (Transpile)
            {
                foreach (var document in compilation.Documents())
                {
                    var filename = compilation.DocumentFileName(document) + ".cs";
                    Console.WriteLine($"Generated: {filename}");

                    var text = document.SyntaxRoot.NormalizeWhitespace().ToFullString();
                    File.WriteAllText(filename, text);
                }

                foreach (var file in compilation.getCSharpFiles())
                {
                    var filename = Path.Combine(_directory, file.Key);
                    if (!File.Exists(file.Key) && !File.Exists(filename))
                    {
                        Console.WriteLine($"Generated: {filename}");

                        var text = file.Value.GetRoot().NormalizeWhitespace().ToFullString();
                        File.WriteAllText(filename, text);
                    }
                }
            }
            else if (result != null)
            {
                var outputPath = OutputPath;
                if (outputPath == null)
                {
                    outputPath = Path.Combine(
                        _directory,
                        Path.GetFileName(_directory));
                }

                if (Path.GetExtension(outputPath) == string.Empty)
                {
                    outputPath = outputPath + (asExe ? ".exe" : ".dll");
                }

                Debug.Assert(outputPath != null);

                outputPath = Path.GetFullPath(outputPath);
                File.WriteAllBytes(outputPath, result.GetBuffer());
                Console.WriteLine($"Successfully built: {outputPath}");
            }

            if (errors.Any())
            {
                foreach (var error in errors)
                {
                    Console.Error.WriteLine(error.ToString());
                }
            }
        }
Example #2
0
        public static TestConcurrentApp Build(string code,
                                              out IEnumerable <Diagnostic> errors,
                                              bool withInterface = false,
                                              bool withRemote    = false)
        {
            errors = null;

            var config = MockInjector(new Options
            {
                GenerateInterface = withInterface,
                GenerateRemote    = withRemote,
            });
            var compilation = new ExcessCompilation();

            compilation.addDocument("concurrent-test", code, config);

            Assembly assembly = compilation.build();

            if (assembly == null)
            {
                errors = compilation.errors();

                //debug
                StringBuilder errorLines = new StringBuilder();
                foreach (var error in errors)
                {
                    errorLines.AppendLine(error.ToString());
                }

                var errorString = errorLines.ToString();
                return(null);
            }

            var types  = new FactoryMap();
            var result = new TestConcurrentApp(types);

            foreach (var type in assembly.GetTypes())
            {
                var attributes = type.CustomAttributes;
                if (!attributes.Any(attr => attr.AttributeType == typeof(ConcurrentAttribute)))
                {
                    continue;
                }

                var typeName = type.ToString();
                if (attributes.Any(attr => attr.AttributeType == typeof(ConcurrentSingleton)))
                {
                    result.AddSingleton(typeName, (IConcurrentObject)Activator.CreateInstance(type));
                    continue;
                }

                var useParameterLess = type.GetConstructors().Length == 0;
                if (!useParameterLess)
                {
                    useParameterLess = type.GetConstructor(new Type[] { }) != null;
                }

                types[typeName] = (app, args) =>
                {
                    if (useParameterLess)
                    {
                        return((IConcurrentObject)Activator.CreateInstance(type));
                    }

                    var ctor = type.GetConstructor(args
                                                   .Select(arg => arg.GetType())
                                                   .ToArray());

                    if (ctor != null)
                    {
                        return((IConcurrentObject)ctor.Invoke(args));
                    }

                    throw new InvalidOperationException("unable to find a constructor");
                };
            }

            return(result);
        }