public static JobScriptCompileResult Compile(JobScriptFile jobScriptFile, string currentPackageFolder) { var jobScriptAssemblyName = BuildScriptAssemblyName(jobScriptFile); var fullAssemblyPath = Path.Combine(currentPackageFolder, jobScriptAssemblyName); if (File.Exists(fullAssemblyPath)) { // Skipped since the same file was already compiled return(new JobScriptCompileResult(fullAssemblyPath)); } // Prepare compiler var codeProvider = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateInMemory = false, OutputAssembly = fullAssemblyPath, IncludeDebugInformation = true, CompilerOptions = String.Format("/lib:\"{0}\",\"Libs\"", currentPackageFolder) }; // Add libraries foreach (var library in jobScriptFile.CompilerLibraries) { options.ReferencedAssemblies.Add(library); } // Compile it var compilerResults = codeProvider.CompileAssemblyFromSource(options, jobScriptFile.JobScript); // Build and return the result object var result = new JobScriptCompileResult(compilerResults); return(result); }
/// <summary> /// Return a job file with the given error /// </summary> private static JobScriptFile CreateWithError(string errorMessage) { var jobFile = new JobScriptFile(null); jobFile.SetError(errorMessage); return(jobFile); }
/// <summary> /// Initializes the instance from all necessary information /// </summary> public bool InitializeFromJobScript(JobScriptFile jobScriptFile, string outputAssembly, HandlerSettings handlerSettings) { // Check if any data changed at all if (JobScript != null && JobScript.Hash == jobScriptFile.Hash) { return(false); } // Set the new values JobScript = jobScriptFile; JobScriptAssembly = outputAssembly; HandlerSettings = handlerSettings; // Notify the running handler of the new value if (_handlerProxy != null) { _handlerProxy.ReplaceJobScriptHash(JobScript.Hash); } // Initialize cron scheduler _cronSchedule = null; NextStartTime = null; if (!String.IsNullOrWhiteSpace(HandlerSettings.Schedule)) { try { _cronSchedule = CrontabSchedule.Parse(HandlerSettings.Schedule); NextStartTime = _cronSchedule.GetNextOccurrence(DateTime.Now); } catch (Exception ex) { _logger.Warn("Handler: Failed to parse Crontab: '{0}' - Ex: {1}", HandlerSettings.Schedule, ex.Message); } } // Initialize idle time _idleInfo = null; if (!String.IsNullOrWhiteSpace(HandlerSettings.IdleTime) && HandlerSettings.IdleTime.Contains("-")) { TimeSpan idleTimeStart; TimeSpan idleTimeEnd; var idleTimeParts = HandlerSettings.IdleTime.Split(new[] { "-" }, StringSplitOptions.RemoveEmptyEntries); var validFrom = TimeSpan.TryParse(idleTimeParts[0], out idleTimeStart); var validTo = TimeSpan.TryParse(idleTimeParts[1], out idleTimeEnd); if (validFrom && validTo) { _idleInfo = new IdleInformation { Start = idleTimeStart, End = idleTimeEnd }; } } return(true); }
/// <summary> /// Parse by string /// </summary> public static JobScriptFile Parse(string jobFileContent) { // Check for empty content if (String.IsNullOrWhiteSpace(jobFileContent)) { return(CreateWithError("Content is empty")); } // Calculate a hash over the entire file var hash = HashCalculator.CalculateMd5Hash(jobFileContent); // Build the file object var jobFile = new JobScriptFile(hash); // Search for compiler libraries var compilerLibrariesString = GetValueWithRegex(jobFileContent, RegCompilerLibraries); foreach (var compilerLibrary in compilerLibrariesString.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)) { jobFile.CompilerLibraries.Add(compilerLibrary); } // Search for dependencies var dependenciesString = GetValueWithRegex(jobFileContent, RegDependencies); foreach (var dependency in dependenciesString.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)) { jobFile.Dependencies.Add(dependency); } // Parse out the package var packageString = GetValueWithRegex(jobFileContent, RegPackageName); jobFile.PackageName = packageString.Trim(); if (String.IsNullOrWhiteSpace(jobFile.PackageName)) { return(CreateWithError("Package name is empty")); } // Parse out the job logic // TODO: Parse out the unnecessary stuff var jobscript = jobFileContent; jobFile.JobScript = jobscript; return(jobFile); }
private static string BuildScriptAssemblyName(JobScriptFile jobScriptFile) { return(String.Format("_job_{0}.dll", jobScriptFile.Hash)); }