use of net.morimekta.providence.util.SimpleTypeRegistry in project providence by morimekta.
the class ProvidenceConfigParserTest method setUp.
@Before
public void setUp() {
registry = new SimpleTypeRegistry();
registry.registerRecursively(Service.kDescriptor);
registry.registerRecursively(Value.kDescriptor);
registry.registerRecursively(RefMerge.kDescriptor);
parser = new ProvidenceConfigParser(registry, false);
}
use of net.morimekta.providence.util.SimpleTypeRegistry in project providence by morimekta.
the class ProvidenceConfigSupplierTest method testReferencedFilesOnUpdatesFromCanonicalFiles.
@Test
public void testReferencedFilesOnUpdatesFromCanonicalFiles() throws IOException {
SimpleTypeRegistry registry = new SimpleTypeRegistry();
registry.registerRecursively(Service.kDescriptor);
watcher = new FileWatcher();
parser = new ProvidenceConfigParser(registry, true);
File service = new File(tmp.getRoot(), "service.config").getAbsoluteFile();
File include = new File(tmp.getRoot(), "include.config").getAbsoluteFile();
File v1 = tmp.newFolder("..dist", "v1").getAbsoluteFile();
File service1 = new File(v1, "service.config").getAbsoluteFile();
File include1 = new File(v1, "include.config").getAbsoluteFile();
File v2 = tmp.newFolder("..dist", "v2").getAbsoluteFile();
File service2 = new File(v2, "service.config").getAbsoluteFile();
File include2 = new File(v2, "include.config").getAbsoluteFile();
// write v1 config
writeContentTo("config.ServicePort {\n" + " port = 8080\n" + "}\n", include1);
writeContentTo("include \"include.config\" as inc\n" + "config.Service {\n" + " name = \"test\"\n" + " http = inc\n" + "}\n", service1);
// write v2 config
writeContentTo("config.ServicePort {\n" + " port = 80\n" + "}\n", include2);
writeContentTo("include \"include.config\" as inc\n" + "config.Service {\n" + " name = \"other\"\n" + " http = inc\n" + "}\n", service2);
Files.createSymbolicLink(service.toPath(), service1.toPath());
Files.createSymbolicLink(include.toPath(), include1.toPath());
ProvidenceConfigSupplier<Service, Service._Field> config = new ProvidenceConfigSupplier<>(service, null, watcher, parser, clock);
Service serviceConfig = config.get();
AtomicReference<Service> serviceRef = new AtomicReference<>(serviceConfig);
assertThat(serviceConfig.getName(), is("test"));
assertThat(serviceConfig.getHttp().getPort(), is((short) 8080));
AtomicBoolean updated = new AtomicBoolean();
ConfigListener<Service, Service._Field> listener = cf -> {
serviceRef.set(cf);
updated.set(true);
};
config.addListener(listener);
File tmpFile = new File(tmp.getRoot(), "..tmp");
Files.createSymbolicLink(tmpFile.toPath(), service2.toPath());
Files.move(tmpFile.toPath(), service.toPath(), StandardCopyOption.REPLACE_EXISTING);
waitAtMost(Duration.ONE_SECOND).untilTrue(updated);
serviceConfig = serviceRef.get();
assertThat(serviceConfig.getName(), is("other"));
assertThat(serviceConfig.getHttp().getPort(), is((short) 80));
}
use of net.morimekta.providence.util.SimpleTypeRegistry in project providence by morimekta.
the class FormatUtils method collectConfigIncludes.
public static void collectConfigIncludes(File rc, Map<String, File> includes) throws IOException {
if (!rc.exists()) {
return;
}
rc = rc.getCanonicalFile();
if (!rc.isFile()) {
throw new ProvidenceConfigException("Rc file is not a file " + rc.getPath());
}
try {
SimpleTypeRegistry registry = new SimpleTypeRegistry();
registry.registerRecursively(ProvidenceTools.kDescriptor);
ProvidenceConfig loader = new ProvidenceConfig(registry);
ProvidenceTools config = loader.getConfig(rc);
if (config.hasIncludes()) {
File basePath = rc.getParentFile();
if (config.hasIncludesBasePath()) {
String base = config.getIncludesBasePath();
if (base.charAt(0) == '~') {
base = System.getenv("HOME") + base.substring(1);
}
basePath = new File(base);
if (!basePath.exists() || !basePath.isDirectory()) {
throw new ProvidenceConfigException("Includes Base path in " + rc.getPath() + " is not a directory: " + basePath);
}
}
for (String path : config.getIncludes()) {
File include = new File(basePath, path);
collectIncludes(include, includes);
}
}
} catch (SerializerException e) {
System.err.println("Config error: " + e.getMessage());
System.err.println(e.asString());
System.err.println();
throw new ArgumentException(e, "Exception when parsing " + rc.getCanonicalFile());
}
}
use of net.morimekta.providence.util.SimpleTypeRegistry in project providence by morimekta.
the class CommonOptions method getConfig.
public ProvidenceTools getConfig() throws ProvidenceConfigException {
SimpleTypeRegistry registry = new SimpleTypeRegistry();
registry.registerRecursively(ProvidenceTools.kDescriptor);
ProvidenceConfig loader = new ProvidenceConfig(registry);
ProvidenceTools config = ProvidenceTools.builder().setGeneratorPaths(ImmutableList.of()).build();
if (rc.isFile()) {
config = loader.getConfig(rc, config);
}
return config;
}
use of net.morimekta.providence.util.SimpleTypeRegistry in project providence by morimekta.
the class ProvidenceConfigSupplierTest method testParentConfigUpdatesPropagaing.
@Test
public void testParentConfigUpdatesPropagaing() throws IOException {
TestConfigSupplier<Database, Database._Field> parent = new TestConfigSupplier<>(Database.builder().setUri("foo").build());
File bar = writeContentTo("config.Database { driver = \"bar\" }", tmp.newFile("bar.config"));
SimpleTypeRegistry registry = new SimpleTypeRegistry();
registry.registerRecursively(Database.kDescriptor);
parser = new ProvidenceConfigParser(registry, false);
ProvidenceConfigSupplier<Database, Database._Field> config = new ProvidenceConfigSupplier<>(bar, parent, watcher, parser, clock);
assertThat(config.get(), is(Database.builder().setUri("foo").setDriver("bar").build()));
ConfigListener<Database, Database._Field> listener = mock(ConfigListener.class);
config.addListener(listener);
parent.testUpdate(Database.builder().setUri("fish").build());
verify(listener).onConfigChange(Database.builder().setUri("fish").setDriver("bar").build());
reset(listener);
config.removeListener(listener);
parent.testUpdate(Database.builder().build());
verifyZeroInteractions(listener);
}
Aggregations