Exemplo n.º 1
0
        private void BuildByDirectory(string dirName, ApplicationBundleInfo curBundleInfo)
        {
            try
            {
                foreach (FileInfo cssFile in new DirectoryInfo(dirName).GetFiles(Settings.CssRazorSearchPattern, SearchOption.AllDirectories))
                {
                    var stylesheet = CssParser.Parse(File.ReadAllText(cssFile.FullName));
                    var children   = (List <IStylesheetNode>)stylesheet.Children;
                    // if contains zero styles
                    if (children.Count == 0)
                    {
                        continue;
                    }

                    string relativeCssPath = Path.GetRelativePath(Settings.ProjectDirectory, cssFile.FullName);
                    string cssFileName     = Path.GetFileNameWithoutExtension(relativeCssPath);

                    RazorDirectory     razorDir = RazorDirectory.Razor;
                    RazorFileExtension razorExt = RazorFileExtension.GenCsharp;

                    RazorFile razorFile  = _razorEngine.FindFile(Settings.ProjectName, cssFileName, razorDir, razorExt);
                    string    @namespace = _razorEngine.GetNamespaceFromFile(Settings.ProjectName, cssFileName, razorDir, razorExt);

                    foreach (IStyleRule rule in children)
                    {
                        if (rule.Type != RuleType.Style)
                        {
                            continue;
                        }

                        if (rule.SelectorText == null)
                        {
                            TextRange range = rule.Owner.StylesheetText.Range;
                            throw new InvalidCssSyntaxException(cssFile.FullName, range.End.Line, range.End.Column);
                        }

                        if (razorFile != null)
                        {
                            rule.SelectorText = BundleHelper.BuildCssClasses(@namespace, cssFileName.Replace(".razor", ""), rule.SelectorText);
                        }
                    }
                    BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, stylesheet);
                    curBundleInfo.CombinedFilesCount++;
                }
                curBundleInfo.Succeed = true;
            }
            catch (IOException ex)
            {
                curBundleInfo.Errors.Add(new Error("IOException", ex.Message));
            }
            catch (InvalidCssSyntaxException ex)
            {
                curBundleInfo.Errors.Add(new Error("CssSyntax", string.Format("invalid css syntax in file by path: {0};\nLine: {1}, Column: {2}", ex.FilePath, ex.Line, ex.Column)));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 検索の方法を定義する
        /// IRequestHandlerで実装することになっている
        /// </summary>
        /// <param name="query">検索条件</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <UpdateResult> Handle(UpdateQuery query, CancellationToken token)
        {
            byte[] razor;
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(query.RazorScript)))
            {
                razor = stream.ToArray();
            }

            // ファイルの保存
            if (!string.IsNullOrWhiteSpace(query.FileName))
            {
                // 入力があれば新規
                // 新規メイン
                var mainData = _db.RazorFiles.FirstOrDefault(x => x.Name == query.MainName && x.Parent == null);
                if (mainData == null)
                {
                    mainData = new RazorFile {
                        Name = query.MainName, Parent = null, Razor = null
                    };
                    _db.RazorFiles.Add(mainData);
                }
                // 新規サブ
                var subData = _db.RazorFiles.FirstOrDefault(x => x.Name == query.SubName && x.Parent == mainData);
                if (subData == null)
                {
                    subData = new RazorFile {
                        Name = query.SubName, Parent = mainData, Razor = null
                    };
                    _db.RazorFiles.Add(subData);
                }
                // 新規ファイル
                var fileData = new RazorFile {
                    Name = query.FileName, Parent = subData, Razor = razor
                };
                _db.RazorFiles.Add(fileData);
            }
            else
            {
                // なければRazorIdを更新
                var data = _db.RazorFiles.First(x => x.Id == long.Parse(query.RazorId));
                data.Razor = razor;
                _db.RazorFiles.Update(data);
            }
            await _db.SaveChangesAsync();

            // 検索結果の格納
            var result = new UpdateResult
            {
                Result = "更新しました。"
            };

            return(await Task.FromResult(result));
        }
Exemplo n.º 3
0
        public override async Task <ComponentBundleInfo> Build()
        {
            OnBundlingStart();
            BuildStopWatch.Reset();
            BuildStopWatch.Start();
            var bundleInfo = new ComponentBundleInfo();

            bundleInfo.PathToBundledAssembly = Settings.AssemblyPath;

            if (_fileManager.TempCssFileExists())
            {
                await _fileManager.ClearTmpCssFileAsync();
            }
            else
            {
                await _fileManager.CreateTmpCssFileAsync();
            }

            try
            {
                // get files in project directory filtered by ".razor.css"
                foreach (FileInfo cssFile in new DirectoryInfo(Settings.ProjectDirectory).GetFiles(Settings.CssRazorSearchPattern, SearchOption.AllDirectories))
                {
                    var stylesheet = CssParser.Parse(File.ReadAllText(cssFile.FullName));
                    var children   = (List <IStylesheetNode>)stylesheet.Children;
                    // if contains zero styles
                    if (children.Count == 0)
                    {
                        continue;
                    }

                    string relativeCssPath = Path.GetRelativePath(Settings.ProjectDirectory, cssFile.FullName);
                    string cssFileName     = Path.GetFileNameWithoutExtension(relativeCssPath);

                    RazorDirectory     razorDir = RazorDirectory.Razor;
                    RazorFileExtension razorExt = RazorFileExtension.GenCsharp;

                    RazorFile razorFile  = _razorEngine.FindFile(Settings.ProjectName, cssFileName, razorDir, razorExt);
                    string    @namespace = _razorEngine.GetNamespaceFromFile(Settings.ProjectName, cssFileName, razorDir, razorExt);

                    foreach (IStyleRule rule in children)
                    {
                        if (rule.Type != RuleType.Style)
                        {
                            continue;
                        }

                        if (rule.SelectorText == null)
                        {
                            TextRange range = rule.Owner.StylesheetText.Range;
                            throw new InvalidCssSyntaxException(cssFile.FullName, range.End.Line, range.End.Column);
                        }

                        if (razorFile != null)
                        {
                            rule.SelectorText = BundleHelper.BuildCssClasses(@namespace, cssFileName.Replace(".razor", ""), rule.SelectorText);
                        }
                    }
                    BundleHelper.AppendStylesToFile(_fileManager.TempStylesFilePath, stylesheet);
                    bundleInfo.CombinedFilesCount++;
                }
                BundleHelper.AddStylesToAssembly(Settings.AssemblyPath, File.ReadAllText(_fileManager.TempStylesFilePath));
                bundleInfo.Succeed = true;
            }
            catch (IOException ex)
            {
                bundleInfo.Errors.Add(new Error("IOException", ex.Message));
            }
            catch (InvalidCssSyntaxException ex)
            {
                bundleInfo.Errors.Add(new Error("CssSyntax", string.Format("invalid css syntax in file by path: {0};\nLine: {1}, Column: {2}", ex.FilePath, ex.Line, ex.Column)));
            }
            BuildStopWatch.Stop();
            bundleInfo.BundlingTime = BuildStopWatch.Elapsed;

            if (bundleInfo.Succeed)
            {
                OnBundlingEnd(bundleInfo);
            }
            else
            {
                OnBundlingError(bundleInfo);
            }

            return(bundleInfo);
        }