The top-level context for the build script.
示例#1
0
 public DirectoryCache(BuildContext context, string directory)
 {
     this.context = context;
     files = Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories)
         .Select(f => f.Remove(0, directory.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))
         .ToLookup(f => Path.GetFileName(f), StringComparer.CurrentCultureIgnoreCase);
 }
示例#2
0
 public BuildHistory(BuildContext context, string historyPath)
 {
     this.context = context;
     path = historyPath;
     if (File.Exists(historyPath))
         history = JsonConvert.DeserializeObject<ConcurrentDictionary<string, HistoryEntry>>(File.ReadAllText(historyPath));
     else
         history = new ConcurrentDictionary<string, HistoryEntry>();
 }
示例#3
0
        public BuildEnvironment(BuildContext context)
        {
            Context = context;
            TempPath = Path.GetTempPath();

            OutputChangeDetection = ChangeDetection.Length;
            InputChangeDetection = ChangeDetection.Length | ChangeDetection.Timestamp | ChangeDetection.Hash;

            InputResolver = Resolvers.PassThrough();
            OutputResolver = Resolvers.PassThrough();
        }
示例#4
0
        internal BuildInstance(BuildContext context, Match match, OutputNode pipeline, bool tempBuild)
        {
            Env = context.Env;
            Match = match;
            Pipeline = pipeline;
            Byproducts = pipeline.Byproducts;
            IsTempBuild = tempBuild;
            Status = BuildStatus.Pending;

            this.context = context;
            TempBuilds = new List<BuildInstance>();
            Dependencies = new List<string>();
            Log = context.Log;
        }
示例#5
0
        public static Assembly Resolver(BuildContext context, ResolveEventArgs args)
        {
            var name = new AssemblyName(args.Name);
            if (name.Name.Contains(".resources"))   // hack to avoid trying to satisfy resource requests
                return null;

            // try to load one of the embedded assemblies
            var dllName = name.Name + ".dll";
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Ampere.Embedded." + dllName);
            if (stream != null)
            {
                var block = new byte[stream.Length];
                stream.Read(block, 0, block.Length);
                stream.Close();

                return Assembly.Load(block);
            }

            // otherwise, try the extra probing paths from the script environment
            if (context != null)
            {
                foreach (var pair in context.Env.ReferencePaths)
                {
                    if (pair.Item2)
                    {
                        var results = Directory.GetFiles(pair.Item1, dllName, SearchOption.AllDirectories);
                        if (results.Length == 1)
                            return Assembly.LoadFrom(results[0]);
                    }
                    else
                    {
                        var dllPath = Path.Combine(pair.Item1, dllName);
                        if (File.Exists(dllPath))
                            return Assembly.LoadFrom(dllPath);
                    }
                }
            }

            // otherwise, defer to the default loader
            return null;
        }
示例#6
0
        public BuildResults Run(string buildScript, int logLevel, bool fullRebuild)
        {
            var context = new BuildContext();
            AppDomain.CurrentDomain.AssemblyResolve += (o, e) => Resolver(context, e);

            var log = new BuildLog();

            // if we weren't given a build script, try to find one in the current directory
            string scriptPath = buildScript;
            if (string.IsNullOrEmpty(scriptPath) || !File.Exists(scriptPath))
            {
                var file = Path.GetDirectoryName(Directory.GetCurrentDirectory());
                if (File.Exists(file + ".csx"))
                    scriptPath = file + ".csx";
                else if (File.Exists(file + ".cs"))
                    scriptPath = file + ".cs";
                else if (File.Exists("build.csx"))
                    scriptPath = "build.csx";
                else if (File.Exists("build.cs"))
                    scriptPath = "build.cs";
                else
                {
                    log.Error("Could not find or open build script.");
                    return null;
                }
            }

            scriptPath = Path.GetFullPath(scriptPath);
            Directory.SetCurrentDirectory(Path.GetDirectoryName(scriptPath));

            // create the script engine
            string historyPath = Path.Combine(DataDirectory, Murmur.Hash(scriptPath, 144) + "_history.dat");
            context.Initialize(historyPath, fullRebuild, log);
            var scriptEngine = new ScriptEngine();
            var session = scriptEngine.CreateSession(context);

            // load plugins and assemblies
            session.AddReference(typeof(BuildContext).Assembly);
            session.AddReference(typeof(Enumerable).Assembly);
            session.AddReference(typeof(HashSet<>).Assembly);
            session.AddReference(typeof(ISet<>).Assembly);

            var code = File.ReadAllText(scriptPath);
            var buildResults = new BuildResults();

            // import default namespaces
            session.ImportNamespace(typeof(BuildContext).Namespace);
            foreach (var n in Namespaces)
                session.ImportNamespace(n);

            try
            {
                // run the script
                var startTime = DateTime.Now;
                log.Write("Running build script ({0})", scriptPath);
                log.Write("Build started at {0}", startTime);
                log.Write("-----------------------------------------------");
                log.Write("");

                session.ExecuteFile(scriptPath);

                context.WaitAll();
                context.Finished();

                log.Write("");
                log.Write("-----------------------------------------------");
                log.Write("Build finished ({0:N2} seconds)", (DateTime.Now - startTime).TotalSeconds);
                log.Write(
                    context.Stats.Failed > 0 ? ConsoleColor.Red : (ConsoleColor?)null,
                    "{0} succeeded, {1} failed, {2} up-to-date",
                    context.Stats.Succeeded,
                    context.Stats.Failed,
                    context.Stats.Skipped
                );
            }
            catch (CompilationErrorException e)
            {
                foreach (var error in e.Diagnostics)
                {
                    var position = error.Location.GetLineSpan(true);
                    log.Error("({0}) {1}", position.StartLinePosition, error.Info.GetMessage());
                }

                return null;
            }

            buildResults.ShouldRunAgain = context.ShouldRunAgain;
            buildResults.ProbedPaths = context.ProbedPaths.Select(p => Path.GetFullPath(p)).ToList();
            buildResults.LoadedPlugins = context.Env.ReferencePaths.Select(t => Path.GetFullPath(t.Item1)).ToList();

            return buildResults;
        }