Example #1
0
        /// <summary>
        /// クラスインターフェースインスタンスの取得
        /// </summary>
        /// <param name="ev">解析結果イベントインスタンス</param>
        /// <param name="filePath">ファイル名</param>
        /// <param name="classIndex">取得対象インデックス(初期値:0)</param>
        /// <returns>クラスインターフェースインスタンス</returns>
        protected IItemClass GetClassInstance(IAnalyzed ev, string filePath, int classIndex = 0)
        {
            // ファイル名の確認
            Assert.Equal(ev.FilePath, filePath);

            // 解析結果の存在確認
            Assert.NotNull(ev.FileRoot);

            // 解析結果の件数確認
            Assert.True(ev.FileRoot.Members.Count >= classIndex + 1, $"{ev.FileRoot.Members.Count} < {classIndex + 1}");

            // IItemClassインスタンスの確認
            Assert.NotNull(ev.FileRoot.Members[classIndex] as IItemClass);

            // IItemClassインスタンスを返す
            return(ev.FileRoot.Members[classIndex] as IItemClass);
        }
Example #2
0
        /// <summary>
        /// 変換処理
        /// </summary>
        /// <param name="analyzed">C#変換結果</param>
        /// <param name="config">設定情報</param>
        /// <param name="indent">インデント数</param>
        /// <returns>変換後のTypeScript文字列</returns>
        public string ConvertTS(IAnalyzed analyzed, Config config, int indent = 0)
        {
            var result = new StringBuilder();

            // 対象ファイルのディレクトリのリストを取得
            var thisDirectory = GetDerectoryList(analyzed.FilePath);

            // 外部ファイル参照を組み立て
            var importFileKeys = new Dictionary <string, List <string> >();

            foreach (var importString in analyzed.FileRoot.OtherFiles)
            {
                // ファイルパス取得
                var filePath = importString.Value;
                if (string.IsNullOrEmpty(filePath))
                {
                    // ファイルパスがない場合は定義済みTSクラス(ファイル出力)
                    filePath = importString.Key;
                }

                // ファイルパスを現地点からの相対パスに変換
                filePath = GetRelativePath(filePath, thisDirectory).Replace(".cs", string.Empty, StringComparison.CurrentCulture);

                // キー名確認とリスト生成
                if (!importFileKeys.ContainsKey(filePath))
                {
                    importFileKeys.Add(filePath, new List <string>());
                }

                // TSクラス名を追加
                var tsClassName = importString.Key;
                var dotIndex    = tsClassName.IndexOf(".", StringComparison.CurrentCulture);
                if (dotIndex > 0)
                {
                    tsClassName = tsClassName.Substring(0, dotIndex);
                }
                importFileKeys[filePath].Add(tsClassName);
            }

            // 外部ファイル参照設定
            var importTargets = importFileKeys.Where(item => !exclusionImportFiles.Contains(item.Key)).OrderBy(item => item.Key);

            if (importTargets.Any())
            {
                result.Append(string.Join(Environment.NewLine, importTargets.Select(item => $"import {{ {string.Join(", ", item.Value.Distinct())} }} from '{item.Key}';")));
                result.AppendLine(Environment.NewLine);
            }

            // C#解析結果を取得
            var targetItems = analyzed.FileRoot.Members;

            // TS変換変換結果を取得
            foreach (var targetItem in targetItems)
            {
                var otherScripts = new List <string>();
                result.Append(ConvertUtility.Convert(targetItem, config, 0, otherScripts));
                result.Append(string.Join(Environment.NewLine, otherScripts));
            }

            // 変換結果を返す
            return(result.ToString());
        }