Esempio n. 1
0
 public static void Log(this ILogStream stream, params LogRecordItem[] items)
 {
     stream.Log(new LogRecord()
     {
         Items = items.ToList()
     });
 }
        public async Task <TransformationResponse> Transform(TransformationRequest sourceRequest)
        {
            var client  = new HttpClient();
            var address = "https://cynxiooa76.execute-api.ap-southeast-2.amazonaws.com";

            Console.WriteLine($"Requesting cloud transform from {address}");

            var request = QuokkaJson.Copy(sourceRequest);

            request.ControllerNames = _runtimeConfiguration.GetControllers?.ToList();

            var payload = JsonConvert.SerializeObject(request);
            var content = new StringContent(payload, Encoding.UTF8, "application/json");
            var data    = await client.PostAsync($"{address}/prod", content);

            Console.WriteLine($"Completed with code: {data.StatusCode}");

            if (data.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new InvalidOperationException($"Request failed: {data.StatusCode}");
            }

            var responseString = await data.Content.ReadAsStringAsync();

            Console.WriteLine($"Content length: {responseString.Length}");

            var response = JsonConvert.DeserializeObject <TransformationResponse>(responseString);

            foreach (var record in response.Logs)
            {
                _logStream.Log(record);
            }

            return(response);
        }
Esempio n. 3
0
        public void Run(string[] args)
        {
            try
            {
                var currentDirectory = Directory.GetCurrentDirectory();
                _directoryTransformation.WatchDirectory(currentDirectory);
            }
            catch (Exception ex)
            {
                Exceptions.RethrowSystemException(ex);

                _logStream.Log(ex);

                Environment.Exit(1);
            }
        }
Esempio n. 4
0
 public static void WriteLine(this ILogStream stream, eContentDomain domain, ConsoleColor foreground, string format = null, params object[] args)
 {
     stream.Log(new LogRecord()
     {
         Items = new List <LogRecordItem>()
         {
             new LogRecordItem()
             {
                 Domain     = domain,
                 Foreground = foreground,
                 Message    = format != null ? string.Format(format, args) : null,
                 NewLine    = true
             }
         }
     });
 }
        public CSharpProjectWatcher(ILogStream logStream)
        {
            _logStream = logStream;

            _fsSubscription = _fsStream
                              .ObserveOn(Scheduler.Default)
                              .Buffer(TimeSpan.FromMilliseconds(500))
                              .Where(list => list.Any())
                              .Subscribe(list => {
                try
                {
                    EventHandler(list);
                }
                catch (Exception ex)
                {
                    Exceptions.RethrowSystemException(ex);

                    _logStream.Log(ex);
                }
            });
        }
Esempio n. 6
0
 public static void Log(this ILogStream stream, params IEnumerable <LogRecordItem>[] items)
 {
     stream.Log(items.SelectMany(i => i).ToArray());
 }
Esempio n. 7
0
 public static void Log(this ILogStream stream, Exception ex)
 {
     stream.Log(DefaultExceptionItems(ex));
 }
Esempio n. 8
0
        public void WatchDirectory(string projectDirectory)
        {
            var projectPath = CSharpProject.GetSingleProjectFromFolder(projectDirectory);

            _logStream.WriteLine(eContentDomain.Public, $"Watching {projectPath}");

            QuokkaConfigLoader.Validate(projectDirectory);

            _logStream.WriteLine(eContentDomain.Public, $"Configration validated");

            Subject <string> fileSteam = new Subject <string>();

            using (var sourceCodeWatcher = new SourceCodeWatcher(_logStream))
            {
                sourceCodeWatcher.SnaphostsStream
                .Throttle(TimeSpan.FromMilliseconds(500))
                .Subscribe(codeSnapshot =>
                {
                    try
                    {
                        using (var scope = _containerFactory.CreateScope())
                        {
                            var logStream             = scope.Resolve <ILogStream>();
                            var projectTransformation = scope.Resolve <IQuokkaProjectTransformation>();

                            codeSnapshot.SourceFiles.ForEach(p => logStream.WriteLine(eContentDomain.Public, p.FullPath));

                            var config = QuokkaConfigLoader.Load(projectDirectory);

                            logStream.WriteLine(eContentDomain.Public, $"{DateTime.Now}, Transforming");

                            var result = projectTransformation.Transform(new TransformationRequest()
                            {
                                Sources        = codeSnapshot.SourceFiles.ToList(),
                                Configurations = config.Configurations
                            }).Result;

                            if (result.Result.Any())
                            {
                                new QuartusProjectTools(logStream)
                                .SaveCodeSnapshot(
                                    new SourceCodeSnapshot(result.Result),
                                    config.Project,
                                    config.ProjectLocation,
                                    config.Configurations.Select(c => c.Name).ToHashSet());
                            }
                            else
                            {
                                logStream.WriteLine(eContentDomain.Public, $"Transformation failed");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Exceptions.RethrowSystemException(ex);

                        _logStream.Log(ex);
                    }
                });

                using (var projectWatcher = new CSharpProjectWatcher(_logStream))
                {
                    projectWatcher.FoldersStream.Subscribe(s =>
                    {
                        s.ForEach(f => _logStream.WriteLine(eContentDomain.Public, f));

                        sourceCodeWatcher.WatchProjectFolders(s);
                    });

                    projectWatcher.WatchProject(projectPath);

                    Console.ReadLine();
                }
            }
        }
Esempio n. 9
0
        public string Transform(string transformed, string existing)
        {
            Dictionary <String, List <String> > mapUserData = new Dictionary <string, List <string> >();
            var userDataTypes = new List <string>()
            {
                "PORTS", "SIGNALS", "ARCHITECTURE"
            };
            List <String> listSourceLines         = new List <String>(transformed.Split(new string[] { "\r\n" }, StringSplitOptions.None));
            List <String> listExistingSourceLines = new List <String>(existing.Split(new string[] { "\r\n" }, StringSplitOptions.None));

            /*
             * {
             *  var beginMapSignature = "[BEGIN USER MAP FOR ";
             *  var endMapSignature = "[END USER MAP FOR ";
             *
             *  Func<string, string> fetchEntityName =
             *      (s) =>
             *      {
             *          return
             *              s
             *              .Replace("-", "") // remove VHDL comments
             *              .Trim() // trim spaces
             *              .Replace(beginMapSignature, "") // cut signature
             *              .Replace("]", "") // remove trailing bracket
             *              .Trim(); // trim spaces
             *      };
             *
             *  Func<List<String>, List<string>> fetchEntityMap =
             *      (codeLines) =>
             *      {
             *          return codeLines
             *                 .Where(l => l.Contains(beginMapSignature))
             *                 .Select(
             *                         l => fetchEntityName(l)
             *                             ).Where(l => !String.IsNullOrWhiteSpace(l))
             *          .ToList();
             *      };
             *
             *  var userEntityMaps = fetchEntityMap(listSourceLines);
             *  userDataTypes.AddRange(userEntityMaps.Select(t => string.Format("MAP FOR {0}", t)));
             *
             *  var userExistingEntityMaps = new List<string>();
             *
             *  int state = 0;
             *  string lastEntityMap = null;
             *  listExistingSourceLines.ForEach(
             *      s =>
             *      {
             *          if( s.Contains( beginMapSignature ))
             *          {
             *              if( 0 == state )
             *              {
             *                  state = 1;
             *                  lastEntityMap = fetchEntityName(s);
             *              }
             *              return;
             *          }
             *
             *          if( s.Contains(endMapSignature))
             *          {
             *              if( state == 2 )
             *              {
             *                  userExistingEntityMaps.Add(lastEntityMap);
             *                  state = 0;
             *              }
             *
             *              return;
             *          }
             *
             *          if( state == 1)
             *          {
             *              state = 2;
             *              return;
             *          }
             *      }
             *      );
             *
             *  var missingMappings = userExistingEntityMaps.Where(l => !userEntityMaps.Contains(l));
             *
             *  if (missingMappings.Any())
             *  {
             *      throw new Exception("Missing mappings: " + string.Join(",", missingMappings));
             *  }
             * }
             */
            try
            {
                Func <string, List <String> > fetchLines =
                    (s) =>
                {
                    var begin = formatBegin(s);
                    var end   = string.Format("[END USER {0}]", s);

                    return
                        (listExistingSourceLines
                         .SkipWhile((l) => !l.Contains(begin))
                         .Skip(1)
                         .TakeWhile((l) => !l.Contains(end))
                         .ToList());
                };

                foreach (var pType in userDataTypes)
                {
                    mapUserData[pType] = fetchLines(pType);
                }
            }
            catch (Exception ex)
            {
                Exceptions.RethrowSystemException(ex);

                // do nothing here
                _logStream.Log(new Exception("Failed to carry over custom code", ex));
            }

            StringBuilder result = new StringBuilder();

            foreach (var pType in mapUserData)
            {
                var pBegin = formatBegin(pType.Key);
                var pIndex = listSourceLines.FindIndex(0, (s) => s.Contains(pBegin));
                if (-1 != pIndex)
                {
                    listSourceLines =
                        listSourceLines
                        .Take(pIndex + 1)
                        .Concat(pType.Value)
                        .Concat(listSourceLines.Skip(pIndex + 1))
                        .ToList();
                }
            }

            listSourceLines.ForEach(s => result.AppendLine(s));

            return(result.ToString());
        }