public IScriptSession CreateSession(IScriptHost host) { // Is Roslyn installed? if (!IsInstalled()) { // Find out which is the latest Roslyn version. _log.Information("Checking what the latest version of Roslyn is..."); var version = GetLatestVersion(); if (version == null) { throw new CakeException("Could not resolve latest version of Roslyn."); } _log.Information("The latest Roslyn version is {0}.", version); _log.Information("Downloading and installing Roslyn..."); Install(version); } // Load Roslyn assemblies dynamically. foreach (var filePath in _paths) { Assembly.LoadFrom(_environment .GetApplicationRoot() .CombineWithFilePath(filePath.GetFilename()) .FullPath); } // Create the session. return(new RoslynNightlyScriptSession(host, _log)); }
public void Initialize() { var root = _environment.GetApplicationRoot(); if (!_installer.IsInstalled(root)) { _log.Information("Downloading and installing Roslyn..."); _installer.Install(root); } }
public IScriptSession CreateSession(IScriptHost host) { // Is Roslyn installed? if (!IsInstalled()) { _log.Information("Downloading and installing Roslyn (experimental)..."); Install(new SemanticVersion(1, 0, 0, "rc2")); } // Load Roslyn assemblies dynamically. foreach (var filePath in _paths) { Assembly.LoadFrom(_environment .GetApplicationRoot() .CombineWithFilePath(filePath.GetFilename()) .FullPath); } // Create the session. return(CreateSession(host, _log)); }
public IScriptSession CreateSession(IScriptHost host) { var root = _environment.GetApplicationRoot(); // Is Roslyn installed? if (!IsInstalled(root)) { _log.Information("Downloading and installing Roslyn..."); Install(root); } // Load Roslyn assemblies dynamically. foreach (var filePath in _paths) { Assembly.LoadFrom(_environment .GetApplicationRoot() .CombineWithFilePath(filePath.GetFilename()) .FullPath); } // Create a new session. return(new RoslynScriptSession(host, _log)); }
private bool Copy(string scriptName, string destinationScriptName) { var applicationRoot = _environment.GetApplicationRoot(); // Get the source file. var sourcePath = applicationRoot.CombineWithFilePath(scriptName); var sourceFile = _fileSystem.GetFile(sourcePath); // Get the destination file. var destinationPath = new FilePath(destinationScriptName).MakeAbsolute(_environment); var destinationFile = _fileSystem.GetFile(destinationPath); // Copy the script. return(Copy(sourceFile, destinationFile)); }
/// <summary> /// Runs the script using the specified script host. /// </summary> /// <param name="host">The script host.</param> /// <param name="scriptPath">The script.</param> /// <param name="arguments">The arguments.</param> public void Run(IScriptHost host, FilePath scriptPath, IDictionary <string, string> arguments) { if (host == null) { throw new ArgumentNullException("host"); } if (scriptPath == null) { throw new ArgumentNullException("scriptPath"); } if (arguments == null) { throw new ArgumentNullException("arguments"); } // Prepare the context. host.Context.Arguments.SetArguments(arguments); host.Context.Environment.WorkingDirectory = scriptPath.MakeAbsolute(host.Context.Environment).GetDirectory(); // Analyze the script file. _log.Verbose("Analyzing build script..."); scriptPath = scriptPath.IsRelative ? scriptPath.MakeAbsolute(_environment) : scriptPath; var result = _analyzer.Analyze(scriptPath.GetFilename()); // Install tools. _log.Verbose("Processing build script..."); var toolsPath = scriptPath.GetDirectory().Combine("tools"); _processor.InstallTools(result, toolsPath); // Install addins. var applicationRoot = _environment.GetApplicationRoot(); var addinRoot = applicationRoot.Combine("Addins").Collapse(); var addinReferences = _processor.InstallAddins(result, addinRoot); foreach (var addinReference in addinReferences) { result.References.Add(addinReference.FullPath); } // Create and prepare the session. var session = _engine.CreateSession(host, arguments); // Load all references. var assemblies = new HashSet <Assembly>(); assemblies.AddRange(_conventions.GetDefaultAssemblies(applicationRoot)); foreach (var reference in result.References) { if (host.Context.FileSystem.Exist((FilePath)reference)) { var assembly = Assembly.LoadFrom(reference); assemblies.Add(assembly); } else { // Add a reference to the session. session.AddReference(reference); } } var aliases = new List <ScriptAlias>(); // Got any assemblies? if (assemblies.Count > 0) { // Find all script aliases. var foundAliases = _aliasFinder.FindAliases(assemblies); if (foundAliases.Count > 0) { aliases.AddRange(foundAliases); } // Add assembly references to the session. foreach (var assembly in assemblies) { session.AddReference(assembly); } } // Import all namespaces. var namespaces = new HashSet <string>(result.Namespaces, StringComparer.Ordinal); namespaces.AddRange(_conventions.GetDefaultNamespaces()); namespaces.AddRange(aliases.SelectMany(alias => alias.Namespaces)); foreach (var @namespace in namespaces.OrderBy(ns => ns)) { session.ImportNamespace(@namespace); } // Execute the script. var script = new Script(result.Namespaces, result.Lines, aliases, result.UsingAliases); session.Execute(script); }
/// <summary> /// Processes the specified line. /// </summary> /// <param name="processor">The script processor.</param> /// <param name="context">The script processor context.</param> /// <param name="currentScriptPath">The current script path.</param> /// <param name="line">The line to process.</param> /// <returns> /// <c>true</c> if the processor handled the line; otherwise <c>false</c>. /// </returns> public override bool Process(IScriptProcessor processor, ScriptProcessorContext context, FilePath currentScriptPath, string line) { if (context == null) { throw new ArgumentNullException("context"); } var tokens = Split(line); var directive = tokens.FirstOrDefault(); if (string.IsNullOrWhiteSpace(directive)) { return(false); } if (!directive.Equals("#addin", StringComparison.OrdinalIgnoreCase)) { return(false); } // Fetch the addin NuGet ID. var addInId = tokens .Select(value => value.UnQuote()) .Skip(1).FirstOrDefault(); if (string.IsNullOrWhiteSpace(addInId)) { return(false); } // Fetch optional NuGet source. var source = tokens .Skip(2) .Select(value => value.UnQuote()) .FirstOrDefault(); // Get the directory path to Cake. var applicationRoot = _environment.GetApplicationRoot(); // Get the addin directory. var addInRootDirectoryPath = applicationRoot .Combine("..\\Addins") .Collapse() .MakeAbsolute(_environment); var addInDirectoryPath = addInRootDirectoryPath.Combine(addInId); var addInRootDirectory = _fileSystem.GetDirectory(addInRootDirectoryPath); // Create the addin directory if it doesn't exist. if (!addInRootDirectory.Exists) { _log.Verbose("Creating addin directory {0}", addInRootDirectoryPath.FullPath); addInRootDirectory.Create(); } // Fetch available addin assemblies. var addInAssemblies = GetAddInAssemblies(addInDirectoryPath); // If no assemblies were found, try install addin from NuGet. if (addInAssemblies.Length == 0) { InstallAddin(addInId, addInRootDirectory, source); addInAssemblies = GetAddInAssemblies(addInDirectoryPath); } // Validate found assemblies. if (addInAssemblies.Length == 0) { throw new CakeException("Failed to find AddIn assemblies"); } // Reference found assemblies. foreach (var assemblyPath in addInAssemblies.Select(assembly => assembly.Path.FullPath)) { _log.Verbose("Addin: {0}, adding Reference {1}", addInId, assemblyPath); context.AddReference(assemblyPath); } return(true); }