/// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="fileName">Script file name</param>
 /// <param name="searchDirs">Extra ScriptLibrary directory </param>
 public ScriptParser(string fileName, string[] searchDirs)
 {
     //if ((CSExecutor.ExecuteOptions.options != null && CSExecutor.options.useSmartCaching) && CSExecutor.ScriptCacheDir == "") //in case if ScriptParser is used outside of the script engine
     if (CSExecutor.ScriptCacheDir == "") //in case if ScriptParser is used outside of the script engine
     {
         CSExecutor.SetScriptCacheDir(fileName);
     }
     Init(fileName, searchDirs);
 }
Exemple #2
0
        internal static string GetScriptedCodeAttributeInjectionCode(string scriptFileName)
        {
            // using SystemWideLock fileLock = new SystemWideLock(scriptFileName, "attr");

            //Infinite timeout is not good choice here as it may block forever but continuing while the file is still locked will
            //throw a nice informative exception.
            // if (Runtime.IsWin)
            //     fileLock.Wait(1000);

            string code = $"[assembly: System.Reflection.AssemblyDescriptionAttribute(@\"{scriptFileName}\")]";

            if (scriptFileName.GetExtension().SameAs(".vb"))
            {
                code = $"<Assembly: System.Reflection.AssemblyDescriptionAttribute(\"{scriptFileName.Replace(@"\", @"\\")}\")>";
            }

            string currentCode = "";

            string file = Path.Combine(CSExecutor.GetCacheDirectory(scriptFileName), scriptFileName.GetFileNameWithoutExtension() + $".attr.g{scriptFileName.GetExtension()}");

            Exception lastError = null;

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    if (File.Exists(file))
                    {
                        using (var sr = new StreamReader(file))
                            currentCode = sr.ReadToEnd();
                    }

                    if (currentCode != code)
                    {
                        string dir = Path.GetDirectoryName(file);

                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        using (var sw = new StreamWriter(file)) //there were reports about the files being locked. Possibly by csc.exe so allow retry

                            sw.Write(code);
                    }
                    break;
                }
                catch (Exception e)
                {
                    lastError = e;
                }
                // Thread.Sleep(200);
            }

            if (!File.Exists(file))
            {
                throw new ApplicationException("Failed to create AttributeInjection file", lastError);
            }

            return(file);
        }