Beispiel #1
0
        private static void SaveNamespace(NamespaceDef namespaceDef, String directory)
        {
            String currentDirectory = directory;

            foreach (String component in namespaceDef.Namespace.Split('.'))
            {
                currentDirectory = Directory.CreateDirectory(Path.Combine(currentDirectory, component)).FullName;
            }

            foreach (FileDef fileDef in namespaceDef.Files.Values)
            {
                CodeWriter writer = new CodeWriter(4);
                writer.WriteLine($"namespace {namespaceDef.Namespace};");

                if (fileDef.Imports.Count > 0)
                {
                    writer.NewLine();
                    foreach (ImportDef importDef in fileDef.Imports)
                    {
                        switch (importDef.Kind)
                        {
                        case ImportKind.Namespace:
                            writer.WriteLine($"import {((ImportNamespaceDef)importDef).Target}.*;");
                            break;

                        case ImportKind.Type:
                            ImportTypeDef importTypeDef = (ImportTypeDef)importDef;
                            writer.WriteLine(importTypeDef.Alias == null
                                                                        ? $"import {importTypeDef.Target.Namespace.Namespace}.{importTypeDef.Target.Name};"
                                                                        : $"import {importTypeDef.Target.Namespace.Namespace}.{importTypeDef.Target.Name} as {importTypeDef.Alias};");
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }

                if (fileDef.Options.Count > 0)
                {
                    writer.NewLine();
                    foreach (NameValuePair option in fileDef.Options)
                    {
                        writer.WriteLine($"option {option.Name} = {option.Value};");
                    }
                }

                SaveTypeScope(fileDef, writer);

                using (StreamWriter stream = new StreamWriter(new FileStream(Path.Combine(currentDirectory, fileDef.FileName), FileMode.Create, FileAccess.ReadWrite)))
                    writer.SaveTo(stream);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates an instance that describes a particular file within the give namespace.
 /// </summary>
 /// <param name="namespace">Instance of the <see cref="NamespaceDef"/> this file belongs to.</param>
 /// <param name="fileName">Name of the file.</param>
 public FileDef([NotNull] NamespaceDef @namespace, [NotNull] String fileName)
 {
     Namespace = @namespace;
     FileName  = fileName;
     Namespace.Files.Add(fileName, this);
 }
Beispiel #3
0
 public ImportNamespaceDef([NotNull] NamespaceDef target)
     : base(ImportKind.Namespace)
 {
     Target = target;
 }