def execute_codegen(self, targets): # Invoke the generator once per target. Because the wire compiler has flags that try to reduce # the amount of code emitted, Invoking them all together will break if one target specifies a # service_writer and another does not, or if one specifies roots and another does not. for target in targets: sources_by_base = self._calculate_sources([target]) if self.codegen_strategy.name() == 'isolated': sources = OrderedSet(target.sources_relative_to_buildroot()) else: sources = OrderedSet( itertools.chain.from_iterable(sources_by_base.values())) if not self.validate_sources_present(sources, [target]): continue relative_sources = OrderedSet() for source in sources: source_root = SourceRoot.find_by_path(source) if not source_root: source_root = SourceRoot.find(target) relative_source = os.path.relpath(source, source_root) relative_sources.add(relative_source) check_duplicate_conflicting_protos(self, sources_by_base, relative_sources, self.context.log) args = ['--java_out={0}'.format(self.codegen_workdir(target))] # Add all params in payload to args if target.payload.get_field_value('no_options'): args.append('--no_options') service_writer = target.payload.service_writer if service_writer: args.append('--service_writer={0}'.format(service_writer)) registry_class = target.payload.registry_class if registry_class: args.append('--registry_class={0}'.format(registry_class)) if target.payload.roots: args.append('--roots={0}'.format(','.join( target.payload.roots))) if target.payload.enum_options: args.append('--enum_options={0}'.format(','.join( target.payload.enum_options))) args.append('--proto_path={0}'.format( os.path.join(get_buildroot(), SourceRoot.find(target)))) args.extend(relative_sources) result = util.execute_java( classpath=self.tool_classpath('wire-compiler'), main='com.squareup.wire.WireCompiler', args=args) if result != 0: raise TaskError( 'Wire compiler exited non-zero ({0})'.format(result))
def test_find(self): # When no source_root is registered, it should just return the path from the address self.assertEqual("tests/foo/bar", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) SourceRoot.register("tests/foo", TestTarget) # After the source root is registered, you should get the source root self.assertEquals("tests/foo", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) with self.assertRaises(TargetDefinitionException): SourceRoot.find(NotTestTarget("//tests/foo/foobar:qux"))
def test_here(self): target = TestTarget("//mock/foo/bar:baz") proxy = AddressableCallProxy(addressable_type=target.get_addressable_type(), build_file=None, registration_callback=None) self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot("mock/foo").here(proxy) self.assertEqual("mock/foo", SourceRoot.find(target))
def test_find(self): # When no source_root is registered, it should just return the path from the address self.assertEqual("tests/foo/bar", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) SourceRoot.register("tests/foo", TestTarget) # After the source root is registered, you should get the source root self.assertEquals("tests/foo", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) with self.assertRaises(TargetDefinitionException): SourceRoot.find(NotTestTarget("//tests/foo/foobar:qux"))
def test_here(self): target = TestTarget("//mock/foo/bar:baz") proxy = AddressableCallProxy(addressable_type=target.get_addressable_type(), build_file=None, registration_callback=None) self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot("mock/foo").here(proxy) self.assertEqual("mock/foo", SourceRoot.find(target))
def test_here(self): class MockParseContext(object): def __init__(self): self.rel_path = "mock/foo" target = TestTarget("//mock/foo/bar:baz") proxy = AddressableCallProxy(addressable_type=target.get_addressable_type(), build_file=None, registration_callback=None) self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot(MockParseContext()).here(proxy) self.assertEqual("mock/foo", SourceRoot.find(target))
def execute_codegen(self, targets): # Invoke the generator once per target. Because the wire compiler has flags that try to reduce # the amount of code emitted, Invoking them all together will break if one target specifies a # service_writer and another does not, or if one specifies roots and another does not. for target in targets: sources_by_base = self._calculate_sources([target]) if self.codegen_strategy.name() == 'isolated': sources = OrderedSet(target.sources_relative_to_buildroot()) else: sources = OrderedSet(itertools.chain.from_iterable(sources_by_base.values())) if not self.validate_sources_present(sources, [target]): continue relative_sources = OrderedSet() for source in sources: source_root = SourceRoot.find_by_path(source) if not source_root: source_root = SourceRoot.find(target) relative_source = os.path.relpath(source, source_root) relative_sources.add(relative_source) check_duplicate_conflicting_protos(self, sources_by_base, relative_sources, self.context.log) args = ['--java_out={0}'.format(self.codegen_workdir(target))] # Add all params in payload to args if target.payload.get_field_value('no_options'): args.append('--no_options') service_writer = target.payload.service_writer if service_writer: args.append('--service_writer={0}'.format(service_writer)) registry_class = target.payload.registry_class if registry_class: args.append('--registry_class={0}'.format(registry_class)) if target.payload.roots: args.append('--roots={0}'.format(','.join(target.payload.roots))) if target.payload.enum_options: args.append('--enum_options={0}'.format(','.join(target.payload.enum_options))) args.append('--proto_path={0}'.format(os.path.join(get_buildroot(), SourceRoot.find(target)))) args.extend(relative_sources) result = util.execute_java(classpath=self.tool_classpath('wire-compiler'), main='com.squareup.wire.WireCompiler', args=args) if result != 0: raise TaskError('Wire compiler exited non-zero ({0})'.format(result))
def test_here(self): class MockParseContext(object): def __init__(self): self.rel_path = "mock/foo" target = TestTarget("//mock/foo/bar:baz") proxy = AddressableCallProxy( addressable_type=target.get_addressable_type(), build_file=None, registration_callback=None) self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot(MockParseContext()).here(proxy) self.assertEqual("mock/foo", SourceRoot.find(target))
def genlang(self, lang, targets): # Invoke the generator once per target. Because the wire compiler has flags that try to reduce # the amount of code emitted, Invoking them all together will break if one target specifies a # service_writer and another does not, or if one specifies roots and another does not. for target in targets: sources_by_base = self._calculate_sources([target]) sources = OrderedSet(itertools.chain.from_iterable(sources_by_base.values())) relative_sources = OrderedSet() for source in sources: source_root = SourceRoot.find_by_path(source) if not source_root: source_root = SourceRoot.find(target) relative_source = os.path.relpath(source, source_root) relative_sources.add(relative_source) check_duplicate_conflicting_protos(self, sources_by_base, relative_sources, self.context.log) if lang != "java": raise TaskError("Unrecognized wire gen lang: {0}".format(lang)) args = ["--java_out={0}".format(self.java_out)] # Add all params in payload to args if target.payload.get_field_value("no_options"): args.append("--no_options") service_writer = target.payload.service_writer if service_writer: args.append("--service_writer={0}".format(service_writer)) registry_class = target.payload.registry_class if registry_class: args.append("--registry_class={0}".format(registry_class)) for root in target.payload.roots: args.append("--roots={0}".format(root)) for enum_option in target.payload.enum_options: args.append("--enum_options={0}".format(enum_option)) args.append("--proto_path={0}".format(os.path.join(get_buildroot(), SourceRoot.find(target)))) args.extend(relative_sources) result = util.execute_java( classpath=self.tool_classpath("wire-compiler"), main="com.squareup.wire.WireCompiler", args=args ) if result != 0: raise TaskError("Wire compiler exited non-zero ({0})".format(result))
def test_register_none(self): self._assert_source_root_empty() SourceRoot.register("tests") self.assertEquals({"tests": OrderedSet()}, SourceRoot.all_roots()) self.assertEquals(OrderedSet(), SourceRoot.types("tests")) self.assertEquals("tests", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) self.assertEquals("tests", SourceRoot.find_by_path("tests/foo/bar"))
def test_register_none(self): self._assert_source_root_empty() SourceRoot.register("tests", ) self.assertEquals({"tests": OrderedSet()}, SourceRoot.all_roots()) self.assertEquals(OrderedSet(), SourceRoot.types("tests")) self.assertEquals("tests", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) self.assertEquals("tests", SourceRoot.find_by_path("tests/foo/bar"))
def check_buildroot(self, buildroot_path): self._assert_source_root_empty() SourceRoot.register(buildroot_path, TestTarget) self.assertEquals({".": OrderedSet([TestTarget])}, SourceRoot.all_roots()) self.assertEquals(OrderedSet([TestTarget]), SourceRoot.types(".")) self.assertEquals(OrderedSet(["."]), SourceRoot.roots(TestTarget)) target = TestTarget("//mock/foo/bar:baz") self.assertEqual("", SourceRoot.find(target))
def check_buildroot(self, buildroot_path): self._assert_source_root_empty() SourceRoot.register(buildroot_path, TestTarget) self.assertEquals({".": OrderedSet([TestTarget])}, SourceRoot.all_roots()) self.assertEquals(OrderedSet([TestTarget]), SourceRoot.types(".")) self.assertEquals(OrderedSet(["."]), SourceRoot.roots(TestTarget)) target = TestTarget("//mock/foo/bar:baz") self.assertEqual("", SourceRoot.find(target))
def _calculate_proto_paths(self, target): """Computes the set of paths that wire uses to lookup imported protos. The protos under these paths are not compiled, but they are required to compile the protos that imported. :param target: the JavaWireLibrary target to compile. :return: an ordered set of directories to pass along to wire. """ proto_paths = OrderedSet() proto_paths.add(os.path.join(get_buildroot(), SourceRoot.find(target))) def collect_proto_paths(dep): if not dep.has_sources(): return for source in dep.sources_relative_to_buildroot(): if source.endswith(".proto"): root = SourceRoot.find_by_path(source) if root: proto_paths.add(os.path.join(get_buildroot(), root)) collect_proto_paths(target) target.walk(collect_proto_paths) return proto_paths
def _calculate_proto_paths(self, target): """Computes the set of paths that wire uses to lookup imported protos. The protos under these paths are not compiled, but they are required to compile the protos that imported. :param target: the JavaWireLibrary target to compile. :return: an ordered set of directories to pass along to wire. """ proto_paths = OrderedSet() proto_paths.add(os.path.join(get_buildroot(), SourceRoot.find(target))) def collect_proto_paths(dep): if not dep.has_sources(): return for source in dep.sources_relative_to_buildroot(): if source.endswith('.proto'): root = SourceRoot.find_by_path(source) if root: proto_paths.add(os.path.join(get_buildroot(), root)) collect_proto_paths(target) target.walk(collect_proto_paths) return proto_paths
def target_base(self): """:returns: the source root path for this target.""" return SourceRoot.find(self)
def format_args_for_target(self, target): """Calculate the arguments to pass to the command line for a single target.""" sources_by_base = self._calculate_sources([target]) if self.codegen_strategy.name() == 'isolated': sources = OrderedSet(target.sources_relative_to_buildroot()) else: sources = OrderedSet( itertools.chain.from_iterable(sources_by_base.values())) if not self.validate_sources_present(sources, [target]): return None relative_sources = OrderedSet() for source in sources: source_root = SourceRoot.find_by_path(source) if not source_root: source_root = SourceRoot.find(target) relative_source = os.path.relpath(source, source_root) relative_sources.add(relative_source) check_duplicate_conflicting_protos(self, sources_by_base, relative_sources, self.context.log) args = ['--java_out={0}'.format(self.codegen_workdir(target))] # Add all params in payload to args if target.payload.get_field_value('no_options'): args.append('--no_options') def append_service_opts(service_type_name, service_type_value, options_values): """Append --service_writer or --service_factory args as appropriate. :param str service_type_name: the target parameter/option prefix :param str service_type_value: class passed to the --service_x= option :param list options_values: string options to be passed with --service_x_opt """ if service_type_value: args.append('--{0}={1}'.format(service_type_name, service_type_value)) if options_values: for opt in options_values: args.append('--{0}_opt'.format(service_type_name)) args.append(opt) # A check is done in the java_wire_library target to make sure only one of --service_writer or # --service_factory is specified. if self.wire_compiler_version < Revision(2, 0): if target.payload.service_factory: raise TaskError( '{spec} used service_factory, which is not available before Wire 2.0. You ' 'should use service_writer instead.'.format( spec=target.address.spec)) append_service_opts('service_writer', target.payload.service_writer, target.payload.service_writer_options) else: if target.payload.service_writer: raise TaskError( '{spec} used service_writer, which is not available after Wire 2.0. You ' 'should use service_factory instead.'.format( spec=target.address.spec)) append_service_opts('service_factory', target.payload.service_factory, target.payload.service_factory_options) registry_class = target.payload.registry_class if registry_class: args.append('--registry_class={0}'.format(registry_class)) if target.payload.roots: args.append('--roots={0}'.format(','.join(target.payload.roots))) if target.payload.enum_options: args.append('--enum_options={0}'.format(','.join( target.payload.enum_options))) if self.wire_compiler_version < Revision(2, 0): args.append('--proto_path={0}'.format( os.path.join(get_buildroot(), SourceRoot.find(target)))) else: # NB(gmalmquist): Support for multiple --proto_paths was introduced in Wire 2.0. for path in self._calculate_proto_paths(target): args.append('--proto_path={0}'.format(path)) args.extend(relative_sources) return args
def target_base(self): return SourceRoot.find(self)
def format_args_for_target(self, target): """Calculate the arguments to pass to the command line for a single target.""" sources_by_base = self._calculate_sources([target]) if self.codegen_strategy.name() == "isolated": sources = OrderedSet(target.sources_relative_to_buildroot()) else: sources = OrderedSet(itertools.chain.from_iterable(sources_by_base.values())) if not self.validate_sources_present(sources, [target]): return None relative_sources = OrderedSet() for source in sources: source_root = SourceRoot.find_by_path(source) if not source_root: source_root = SourceRoot.find(target) relative_source = os.path.relpath(source, source_root) relative_sources.add(relative_source) check_duplicate_conflicting_protos(self, sources_by_base, relative_sources, self.context.log) args = ["--java_out={0}".format(self.codegen_workdir(target))] # Add all params in payload to args if target.payload.get_field_value("no_options"): args.append("--no_options") def append_service_opts(service_type_name, service_type_value, options_values): """Append --service_writer or --service_factory args as appropriate. :param str service_type_name: the target parameter/option prefix :param str service_type_value: class passed to the --service_x= option :param list options_values: string options to be passed with --service_x_opt """ if service_type_value: args.append("--{0}={1}".format(service_type_name, service_type_value)) if options_values: for opt in options_values: args.append("--{0}_opt".format(service_type_name)) args.append(opt) # A check is done in the java_wire_library target to make sure only one of --service_writer or # --service_factory is specified. if self.wire_compiler_version < Revision(2, 0): if target.payload.service_factory: raise TaskError( "{spec} used service_factory, which is not available before Wire 2.0. You " "should use service_writer instead.".format(spec=target.address.spec) ) append_service_opts("service_writer", target.payload.service_writer, target.payload.service_writer_options) else: if target.payload.service_writer: raise TaskError( "{spec} used service_writer, which is not available after Wire 2.0. You " "should use service_factory instead.".format(spec=target.address.spec) ) append_service_opts( "service_factory", target.payload.service_factory, target.payload.service_factory_options ) registry_class = target.payload.registry_class if registry_class: args.append("--registry_class={0}".format(registry_class)) if target.payload.roots: args.append("--roots={0}".format(",".join(target.payload.roots))) if target.payload.enum_options: args.append("--enum_options={0}".format(",".join(target.payload.enum_options))) if self.wire_compiler_version < Revision(2, 0): args.append("--proto_path={0}".format(os.path.join(get_buildroot(), SourceRoot.find(target)))) else: # NB(gmalmquist): Support for multiple --proto_paths was introduced in Wire 2.0. for path in self._calculate_proto_paths(target): args.append("--proto_path={0}".format(path)) args.extend(relative_sources) return args
def test_here(self): target = TestTarget("//mock/foo/bar:baz") self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot("mock/foo").here() self.assertEqual("mock/foo", SourceRoot.find(target))
def target_base(self): """:returns: the source root path for this target.""" return SourceRoot.find(self)
def test_here(self): target = TestTarget("//mock/foo/bar:baz") self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot("mock/foo").here() self.assertEqual("mock/foo", SourceRoot.find(target))
def check_here_buildroot(self, buildroot_path): target = TestTarget("//mock/foo/bar:baz") self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot(buildroot_path).here() self.assertEqual("", SourceRoot.find(target))
def target_base(self): return SourceRoot.find(self)
def check_here_buildroot(self, buildroot_path): target = TestTarget("//mock/foo/bar:baz") self.assertEqual("mock/foo/bar", SourceRoot.find(target)) SourceRoot(buildroot_path).here() self.assertEqual("", SourceRoot.find(target))