Ejemplo n.º 1
0
	def write(self, app):

		now = datetime.datetime.now()
		fileName = app.protocolName + ".h"

		with open(fileName, "w") as file:

			# header
			file.write("//\n// " + fileName + "\n//\n")
			file.write("// DO NOT EDIT!\n")
			file.write("// THIS FILE WAS GENERATED BY appFactoryFactory AT " + str(now) + "\n")
			file.write("//\n\n")
			file.write("#import <Foundation/Foundation.h>\n")
			file.write("#import \"InjectableAppDelegate.h\"\n\n")

			for externalHeaderFile in app.externalHeaderFilesForProtocolHeader:
				file.write("#import \"" + externalHeaderFile + "\"\n")

			file.write("\n")

			# app factory protocol
			file.write("@protocol " + app.protocolName + "\n")
			file.write("- (void)injectDependenciesIntoAppDelegate:(id<InjectableAppDelegate>)anInjectableAppDelegate;\n")
			file.write("@end\n")

			file.write("\n\n//\n// instance factory protocols\n//\n")

			for component in app.components:
				if not component.isInstanceFactoryProduct:
					continue

				file.write("\n@protocol " + component.protocolName + ";\n")
				operationalDependencies = InstanceFactoryLib.getOperationalDependencies(app, component)
				for dependency in operationalDependencies:
					if dependency[0] == ":":
						dependency = dependency[1:]
					type = dependency.split(":")[1].strip().split("(")[1].split(")")[0].strip()
					if type.startswith("id<") and type.endswith(">"):
						file.write("@protocol " + type[3:-1] + ";\n")
						# we are not handling the case where the type is a specific class implementing a protocol -- defering until needed
					else:
						if type.endswith("*"):
							type = type[0:-1].strip()
							file.write("@class " + type + ";\n")
				file.write("@protocol " + component.capitalizedName + "InstanceFactory\n")

				file.write(InstanceFactoryLib.factoryMethodSignature(app, component))
				file.write(";\n@end\n")
Ejemplo n.º 2
0
    def writeInstanceFactoryMethods(self, file, app, componentMap):
        for component in app.components:
            if not component.isInstanceFactoryProduct:
                continue

            file.write(InstanceFactoryLib.factoryMethodSignature(app, component))
            file.write("\n{\n")

            file.write("\t" + component.className + "* impl = [[" + component.className + " alloc] init")

            initDependencies = []
            for dependency in component.dependencies:
                if dependency[0] == ":":
                    initDependencies.append(dependency[1:])

            init = ""

            if initDependencies:
                init = "With"
                firstDesignatedInitializerParameter = True
                for dependency in initDependencies:
                    isInstanceFactory = False
                    if dependency.endswith("InstanceFactory"):
                        isInstanceFactory = True
                        dependency = dependency[0 : len(dependency) - len("InstanceFactory")]

                    if not componentMap.has_key(dependency):
                        if dependency[0] == "*":
                            tokens = dependency[1:].split(":")
                            name = tokens[0].strip()
                            name = name[0].lower() + name[1:]
                            if firstDesignatedInitializerParameter:
                                init += name[0].upper() + name[1:]
                            else:
                                init += name
                            init += ":"
                            init += "op_" + name
                        else:
                            init += dependency
                    else:
                        dependencyComponent = componentMap[dependency]
                        if firstDesignatedInitializerParameter:
                            init += dependencyComponent.capitalizedName
                        else:
                            init += dependencyComponent.name
                        init += ":"
                        init += self.constructDependencyAccessor(componentMap, dependency, isInstanceFactory)

                    init += " "
                    firstDesignatedInitializerParameter = False

            init = init.strip()
            file.write(init)

            file.write("];\n")

            self.writeDependencySetterInjections(file, componentMap, component, "impl", 1)

            file.write("\treturn impl;\n}\n")