TypewriterCli icon indicating copy to clipboard operation
TypewriterCli copied to clipboard

Error when try to import and use external classes "using" in template .tst file

Open rafalkasa opened this issue 7 years ago • 1 comments

Hi @barestan

I'm really impressed by your work and that you try to give the community the possibility to use Typewriter with JetBrains Rider. This is amazing and very appreciated 😄

${
	using Typewriter.Extensions.Types;
	using System.Text.RegularExpressions;
	using System.Diagnostics;

	string ToKebabCase(string typeName){
		return  Regex.Replace(typeName, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])","-$1", RegexOptions.Compiled)
					 .Trim().ToLower();
	}

	string Imports(Class c){
	  string inheritImports = (c.BaseClass!=null) ? ( "import { " + CleanupName(c.BaseClass).ToString() +" } from './" +ToKebabCase(CleanupName(c.BaseClass)) +"';") + Environment.NewLine :"";
	  string emptyEnumeImports =  (!String.IsNullOrEmpty(inheritImports)) ? Environment.NewLine : "";
	  string emptyClassImports =  (!String.IsNullOrEmpty(inheritImports)) ? Environment.NewLine : "";
	  string enumeImports = (!String.IsNullOrEmpty(EnumeImports(c))) ? EnumeImports(c) + Environment.NewLine : emptyEnumeImports;
	  string classImports = (!String.IsNullOrEmpty(ClassImports(c))) ? ClassImports(c) + Environment.NewLine : emptyClassImports;
	  return inheritImports + classImports + enumeImports;
	}
...

Could you help me with my *.tst file, I have some using in my template file *.tst when I run TypewriterCLI I received errors as below:

Template error: CS0103 The name "Regex" does not exist in the current context
Error: The name "Regex" does not exist in the current context
Template error: CS0103 The name "RegexOptions" does not exist in the current context
Error: The name "RegexOptions" does not exist in the current context
Template error: CS0122 'The "Environment" element is unavailable due to its protection level.
Error: 'The "Environment" element is unavailable due to its level of protection.
Template error: CS0122 'The "Environment" element is unavailable due to its protection level.
Error: 'The "Environment" element is unavailable due to its level of protection.
Template error: CS0122 'The "Environment" element is unavailable due to its protection level.
Error: 'The "Environment" element is unavailable due to its level of protection.
Template error: CS0122 'The "Environment" element is unavailable due to its protection level.
Error: 'The "Environment" element is unavailable due to its level of protection.
Template error: CS0122 'The "Environment" element is unavailable due to its protection level.
Error: 'The "Environment" element is unavailable due to its level of protection.
Error: Template compilation errors. Template:. \ .. \ TypeScriptTemplate.tst

rafalkasa avatar Jan 20 '19 21:01 rafalkasa

Maybe this will help others. I resolved my issue importing and adding assemblies in class ShadowWorkspace:

namespace Typewriter.Lexing.Roslyn
{
    internal class ShadowWorkspace : Workspace
    {
        private static int _counter;
        private static readonly Assembly[] DefaultReferences =
        {
            typeof (int).Assembly, // mscorelib
            typeof (Uri).Assembly, // System
            typeof (Enumerable).Assembly, // System.Core
            typeof (Regex).Assembly, // System.Text.RegularExpressions
            typeof (Environment).Assembly, // System.Diagnostics
            //typeof (XmlReader).Assembly, // System.Xml
            //typeof (XDocument).Assembly, // System.Xml.Linq
            typeof (RuntimeBinderException).Assembly, // Microsoft.CSharp
            typeof (Class).Assembly // Typewriter.CodeModel
        };
...

rafalkasa avatar Jan 20 '19 22:01 rafalkasa