private static void SendGeneric(IList <XamlFileMeta> files, XamlProjectMeta previousMeta, string componentPrefix)
        {
            try {
                var buffer     = GetBuffer(files, previousMeta, componentPrefix);
                var bufferSent = false;

                try {
                    bufferSent = TrySendTcp(buffer);
                } catch (Exception e) {
                    Debug.WriteLine("Could not send to TCP server on 127.0.0.1: " + e.Message);
                }

                if (!bufferSent)
                {
                    using (var udpClient = new UdpClient()) {
                        udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                        udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
                        udpClient.Send(buffer, buffer.Length, new IPEndPoint(IPAddress.Broadcast, SendPort));
                    }
                }
            } catch (Exception e) {
                Debug.WriteLine("Sending runtime update failed: " + e);
                throw;
            }
        }
Ejemplo n.º 2
0
        private static void GenerateMetaFile(string fullOutputPath, XamlProjectMeta projectMeta)
        {
            var metaFilename = Path.Combine(fullOutputPath, "ammy.meta");

            using (var writer = new StreamWriter(metaFilename, false)) {
                var serializer = new XmlSerializer(typeof(XamlProjectMeta));
                serializer.Serialize(writer, projectMeta);
            }
        }
        private static List <string> GetPropertiesToReset(XamlFileMeta file, XamlProjectMeta previousMeta)
        {
            var previousFileMeta = previousMeta.Files.FirstOrDefault(f => f.Filename.Equals(file.Filename, StringComparison.InvariantCultureIgnoreCase));

            if (previousFileMeta == null)
            {
                return(new List <string>());
            }

            return(previousFileMeta.Properties
                   .SelectMany(m => new[] { m.PropertyType.ToString() + "|" + m.FullName })
                   .Distinct()
                   .ToList());
        }
        public static void Send(string platformName, IList <XamlFileMeta> files, XamlProjectMeta previousMeta, string componentPrefix)
        {
            if (files.Count == 0)
            {
                return;
            }

            if (platformName == "WPF")
            {
                SendGeneric(files, previousMeta, componentPrefix);
            }
            else if (platformName == "XamarinForms")
            {
                SendXamarinForms(files, previousMeta, componentPrefix);
            }
        }
        private static byte[] GetBuffer(IList <XamlFileMeta> files, XamlProjectMeta previousMeta, string componentPrefix)
        {
            var header = new byte[] { 0xbe, 0xef };
            var footer = new byte[] { 0xff };
            var result = header.ToList();

            result.Add((byte)files.Count);

            foreach (var file in files)
            {
                byte[] buffer;

                var extension = Path.GetExtension(file.FilePath);
                if (extension != null && extension.Equals(".xaml", StringComparison.InvariantCultureIgnoreCase))
                {
                    buffer = Encoding.Unicode.GetBytes(File.ReadAllText(file.FilePath));
                }
                else
                {
                    buffer = File.ReadAllBytes(file.FilePath);
                }

                var filename = file.Filename;
                var id       = componentPrefix + filename.Replace("\\", "/").TrimStart('/');

                result.AddRange(CreateMarkupBuffer(buffer, id));

                var propertyList     = GetPropertiesToReset(file, previousMeta);
                var propertiesBuffer = CreatePropertiesBuffer(propertyList);

                result.AddRange(propertiesBuffer);
            }

            result.AddRange(footer);

            var toSend = result.ToArray();

            return(toSend);
        }
Ejemplo n.º 6
0
        private void GenerateTopNodes(XamlProjectMeta projectMeta)
        {
            foreach (var file in _files)
            {
                var topWithNode = ((Start)file.Ast).Top as TopWithNode;

                if (topWithNode == null)
                {
                    continue;
                }

                if (!topWithNode.IsXamlEvaluated)
                {
                    throw new Exception("Xaml node is not evaluated in file " + file.Meta.Filename);
                }

                var functionRefs     = topWithNode.TopNode.FunctionRefScope.GetSymbols();
                var rootFunctionRefs = functionRefs.SelectMany(fr => new[] { fr.FirstDeclarationOrDefault })
                                       .OfType <ContentFunctionRef>()
                                       .Where(cfr => cfr.IsParentEvaluated && cfr.Parent == topWithNode.TopNode)
                                       .SelectMany(cfr => new[] { cfr.FunctionRef.Symbol.FirstDeclarationOrDefault })
                                       .OfType <GlobalDeclaration.ContentFunction>()
                                       .SelectMany(cf => cf.Members);

                var xamlNode      = (XamlNode)topWithNode.Xaml;
                var xaml          = xamlNode.Build();
                var xamlHash      = MD5.Create().ComputeHash(Encoding.Unicode.GetBytes(xaml));
                var propertyMetas = GetPropertyMetas(topWithNode.TopNode.Members);
                var propertyMetasFromFunctions = GetPropertyMetas(rootFunctionRefs);

                // Generate imlementation
                var ammyProject = _compileResult.AmmyProject;

                var outputFileSuffix = ammyProject.Platform.OutputFileSuffix;
                var xamlFilePath     = Path.ChangeExtension(file.OutputFilename, outputFileSuffix + ".xaml");


                for (int i = 0; i < 3; i++)
                {
                    try {
                        File.WriteAllText(xamlFilePath, xaml);
                        break;
                    } catch (IOException) {
                        // Might throw access denied exception, so retry few times
                        Thread.Sleep(50);
                    }
                }

                var metaFilePath = xamlFilePath;

                if (ammyProject.Platform is WpfPlatform)
                {
                    var projectDir       = ammyProject.FsProject.ProjectDir;
                    var objDir           = Path.Combine(projectDir, ammyProject.OutputPath);
                    var relativeFile     = file.OutputFilename.ToRelativeFile(projectDir);
                    var bamlRelativeFile = Path.ChangeExtension(relativeFile, outputFileSuffix + ".baml");

                    metaFilePath = Path.Combine(objDir, bamlRelativeFile);
                }

                projectMeta.Files.Add(new XamlFileMeta {
                    FilePath   = metaFilePath,
                    Filename   = xamlFilePath.ToRelativeFile(_projectPath),
                    Hash       = BitConverter.ToString(xamlHash),
                    Properties = propertyMetas.Concat(propertyMetasFromFunctions)
                                 .ToList()
                });

                _compileResult.GeneratedXamlFiles.Add(xamlFilePath);
            }
        }
 private static void SendXamarinForms(IList <XamlFileMeta> files, XamlProjectMeta previousMeta, string componentPrefix)
 {
     SendGeneric(files, previousMeta, componentPrefix);
 }