Exemplo n.º 1
0
        /// <summary>
        /// Compiles the specified script text.
        /// </summary>
        /// <param name="scriptText">The script text.</param>
        /// <param name="scriptFile">The script file.</param>
        /// <param name="info">The information.</param>
        /// <returns>The method result.</returns>
        override protected (byte[] asm, byte[] pdb) Compile(string scriptText, string scriptFile, CompileInfo info)
        {
            // Debug.Assert(false);
            string tempScriptFile = null;
            string injection_file = null;

            try
            {
                if (scriptFile == null)
                {
                    tempScriptFile = CSScript.GetScriptTempFile();
                    File.WriteAllText(tempScriptFile, scriptText);
                }

                var project = Project.GenerateProjectFor(tempScriptFile ?? scriptFile);
                var refs    = project.Refs.Concat(this.GetReferencedAssembliesFiles()).Distinct().ToArray();
                var sources = project.Files;

                if (info?.AssemblyFile != null)
                {
                    injection_file = CoreExtensions.GetScriptedCodeAttributeInjectionCode(info.AssemblyFile);
                    sources        = sources.Concat(new[] { injection_file }).ToArray();
                }

                (byte[], byte[])result = CompileAssemblyFromFileBatch_with_Csc(sources, refs, info?.AssemblyFile, this.IsDebug, info);

                return(result);
            }
            finally
            {
                if (this.IsDebug)
                {
                    CSScript.NoteTempFile(tempScriptFile);
                }
                else
                {
                    tempScriptFile.FileDelete(rethrow: false);
                }

                injection_file.FileDelete(rethrow: false);

                CSScript.StartPurgingOldTempFiles(ignoreCurrentProcessScripts: true);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compiles the specified script text.
        /// </summary>
        /// <param name="scriptText">The script text.</param>
        /// <param name="scriptFile">The script file.</param>
        /// <param name="info">The information.</param>
        /// <returns>The method result.</returns>
        override protected (byte[] asm, byte[] pdb) Compile(string scriptText, string scriptFile, CompileInfo info)
        {
            string tempScriptFile = null;
            string injection_file = null;

            try
            {
                if (scriptFile == null)
                {
                    tempScriptFile = CSScript.GetScriptTempFile();
                    File.WriteAllText(tempScriptFile, scriptText);
                }

                var project = Project.GenerateProjectFor(tempScriptFile ?? scriptFile);
                var refs    = project.Refs.Concat(this.GetReferencedAssembliesFiles()).Distinct().ToArray();
                var sources = project.Files;

                if (info?.AssemblyFile != null)
                {
                    injection_file = CoreExtensions.GetScriptedCodeAttributeInjectionCode(info.AssemblyFile);
                    sources        = sources.Concat(new[] { injection_file }).ToArray();
                }

                int scriptHash = 0;

                if (IsCachingEnabled)
                {
                    var hashableCode = new StringBuilder();
                    hashableCode.Append($"{scriptText}.{scriptFile?.GetFullPath()}");
                    foreach (string dependencyScript in sources)
                    {
                        hashableCode.Append(File.ReadAllText(dependencyScript).GetHashCode());
                    }

                    scriptHash = $"{scriptText}.{scriptFile?.GetFullPath()}".GetHashCode(); // not very sophisticated (e.g. not all ref asms are
                                                                                            // included in hashing)
                                                                                            // but adequate

                    if (scriptCache.ContainsKey(scriptHash))
                    {
                        return(scriptCache[scriptHash]);
                    }
                }

                (byte[], byte[])result = CompileAssemblyFromFileBatch_with_Csc(sources, refs, info?.AssemblyFile, this.IsDebug, info);

                if (IsCachingEnabled)
                {
                    scriptCache[scriptHash] = result;
                }

                return(result);
            }
            finally
            {
                if (this.IsDebug)
                {
                    CSScript.NoteTempFile(tempScriptFile);
                }
                else
                {
                    tempScriptFile.FileDelete(rethrow: false);
                }

                injection_file.FileDelete(rethrow: false);

                CSScript.StartPurgingOldTempFiles(ignoreCurrentProcessScripts: true);
            }
        }