protected virtual void EnsureAssemblyName(Project project)
        {
            if (ProjectProperties.AssemblyName == null && _shouldReadProjectFile)
            {
                var property = (from n in project.AllEvaluatedProperties
                                where n.Name == ProjectPropertyNames.ASSEMBLY_NAME_PROP
                                select n).LastOrDefault();

                if (property != null)
                {
                    ProjectProperties.AssemblyName = property.EvaluatedValue;
                }
            }

            if (string.IsNullOrWhiteSpace(ProjectProperties.AssemblyName))
            {
                TranslatorException.Throw("Unable to determine assembly name");
            }
        }
        private string ReadProperty(Project doc, string name, bool fineIfMissing, Contract.ConfigHelper configHelper)
        {
            var node = (from n in doc.AllEvaluatedProperties
                        where string.Compare(n.Name, name, true) == 0
                        select n).LastOrDefault();

            if (node is null)
            {
                if (fineIfMissing)
                {
                    return(null);
                }

                TranslatorException.Throw("Unable to determine " + name + " in the project file with conditions " + EvaluationConditionsAsString());
            }

            var value = node.EvaluatedValue;

            value = configHelper.ConvertPath(value);

            return(value);
        }
Exemple #3
0
        public virtual HashSet <string> GetParentTypes(IDictionary <string, TypeDefinition> allTypes)
        {
            var result = new HashSet <string>();

            foreach (var type in allTypes.Values)
            {
                if (type.BaseType != null)
                {
                    string parentName = type.BaseType.FullName.LeftOf('<').Replace('`', JS.Vars.D);

                    if (!allTypes.ContainsKey(parentName))
                    {
                        TranslatorException.Throw("Unknown type {0}", parentName);
                    }

                    if (!result.Contains(parentName))
                    {
                        result.Add(parentName);
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Validates project and namespace names against conflicts with H5 namespaces.
        /// </summary>
        /// <param name="project">XDocument reference of the .csproj file.</param>
        private void ValidateProject(Project project)
        {
            var valid         = true;
            var failList      = new HashSet <string>();
            var failNodeList  = new List <ProjectProperty>();
            var combined_tags = from x in project.AllEvaluatedProperties
                                where x.Name == ProjectPropertyNames.ROOT_NAMESPACE_PROP || x.Name == ProjectPropertyNames.ASSEMBLY_NAME_PROP
                                select x;

            if (!AssemblyInfo.Assembly.EnableReservedNamespaces)
            {
                foreach (var tag in combined_tags)
                {
                    if (tag.EvaluatedValue == CS.NS.H5)
                    {
                        valid = false;
                        if (!failList.Contains(tag.EvaluatedValue))
                        {
                            failList.Add(tag.EvaluatedValue);
                            failNodeList.Add(tag);
                        }
                    }
                }
            }

            if (!valid)
            {
                var offendingSettings = "";
                foreach (var tag in failNodeList)
                {
                    offendingSettings += "Line " + tag.Xml.Location.File + " (" + tag.Xml.Location.Line + "): <" + tag.Xml.Name + ">" +
                                         tag.UnevaluatedValue + "</" + tag.Xml.Name + ">\n";
                }

                throw new TranslatorException("'H5' name is reserved and may not " +
                                              "be used as project names or root namespaces.\n" +
                                              "Please verify your project settings and rename where it applies.\n" +
                                              "Project file: " + Location + "\n" +
                                              "Offending settings:\n" + offendingSettings
                                              );
            }

            var outputType = ProjectProperties.OutputType;

            if (outputType == null && _shouldReadProjectFile)
            {
                var projectType = (from n in project.AllEvaluatedProperties
                                   where n.Name == ProjectPropertyNames.OUTPUT_TYPE_PROP
                                   select n).LastOrDefault();

                if (projectType != null)
                {
                    outputType = projectType.EvaluatedValue;
                }
            }

            if (outputType != null && string.Compare(outputType, SupportedProjectType, true) != 0)
            {
                TranslatorException.Throw("Project type ({0}) is not supported, please use Library instead of {0}", outputType);
            }
        }
Exemple #5
0
        public virtual void CheckObjectLiteral(TypeDefinition type, ITranslator translator)
        {
            if (!IsObjectLiteral(type))
            {
                return;
            }

            var objectCreateMode = GetObjectCreateMode(type);

            if (objectCreateMode == 0)
            {
                var ctors = type.GetConstructors();

                foreach (var ctor in ctors)
                {
                    foreach (var parameter in ctor.Parameters)
                    {
                        if (parameter.ParameterType.FullName == "H5.ObjectCreateMode")
                        {
                            TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_PLAIN_NO_CREATE_MODE_CUSTOM_CONSTRUCTOR, type);
                        }

                        if (parameter.ParameterType.FullName == "H5.ObjectInitializationMode")
                        {
                            continue;
                        }

                        TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_PLAIN_CUSTOM_CONSTRUCTOR, type);
                    }
                }
            }

            if (type.IsInterface)
            {
                if (type.HasMethods && type.Methods.GroupBy(m => m.Name).Any(g => g.Count() > 1))
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_NO_OVERLOAD_METHODS, type);
                }

                if (type.HasEvents)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_NO_EVENTS, type);
                }
            }
            else
            {
                if (type.Methods.Any(m => !m.IsRuntimeSpecialName && m.Name.Contains(".") && !m.Name.Contains("<")) ||
                    type.Properties.Any(m => !m.IsRuntimeSpecialName && m.Name.Contains(".") && !m.Name.Contains("<")))
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_NO_EXPLICIT_IMPLEMENTATION, type);
                }
            }

            if (type.BaseType != null)
            {
                TypeDefinition baseType = null;
                try
                {
                    baseType = type.BaseType.Resolve();
                }
                catch (Exception)
                {
                }

                if (objectCreateMode == 1 && baseType != null && baseType.FullName != "System.Object" && baseType.FullName != "System.ValueType" && GetObjectCreateMode(baseType) == 0)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_CONSTRUCTOR_INHERITANCE, type);
                }

                if (objectCreateMode == 0 && baseType != null && GetObjectCreateMode(baseType) == 1)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_PLAIN_INHERITANCE, type);
                }
            }

            if (type.Interfaces.Count > 0)
            {
                foreach (var @interface in type.Interfaces)
                {
                    TypeDefinition iDef = null;
                    try
                    {
                        iDef = @interface.InterfaceType.Resolve();
                    }
                    catch (Exception)
                    {
                    }

                    if (iDef != null && iDef.FullName != "System.Object" && !IsObjectLiteral(iDef))
                    {
                        TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_INHERITANCE, type);
                    }
                }
            }

            if (objectCreateMode == 0)
            {
                var hasVirtualMethods = false;

                foreach (MethodDefinition method in type.Methods)
                {
                    if (AttributeHelper.HasCompilerGeneratedAttribute(method))
                    {
                        continue;
                    }

                    if (method.IsVirtual && !(method.IsSetter || method.IsGetter))
                    {
                        hasVirtualMethods = true;
                        break;
                    }
                }

                if (hasVirtualMethods)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_NO_VIRTUAL_METHODS, type);
                }
            }
        }