Skip to content

Commit

Permalink
add server embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Dec 11, 2024
1 parent 46d0012 commit f46aafc
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Rewrite/Rewrite.Sources.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.CSharp.Tests", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Test", "src\Rewrite.Test\Rewrite.Test.csproj", "{D3F48947-C702-4D1A-B6EE-96B89D13953D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Server", "src\Rewrite.Server\Rewrite.Server.csproj", "{D3F48947-C702-4D1A-B6EE-96B89D13953E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C7A4A8F9-0343-4A3E-AB77-101C458C23B5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{7B268266-B9CD-4D85-ABC5-3B2C4611FD74}"
Expand Down Expand Up @@ -93,6 +95,10 @@ Global
{D3F48947-C702-4D1A-B6EE-96B89D13953D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953D}.Release|Any CPU.Build.0 = Release|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Release|Any CPU.Build.0 = Release|Any CPU
{0E364862-1ACA-4C94-8EA0-23724FF67AA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E364862-1ACA-4C94-8EA0-23724FF67AA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E364862-1ACA-4C94-8EA0-23724FF67AA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -130,6 +136,7 @@ Global
{5DD7D88A-5A10-457D-B1A2-30F7CF67AD9F} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{C7E280C5-E841-4690-8AA3-653ECBC7C78B} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{D3F48947-C702-4D1A-B6EE-96B89D13953D} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{D3F48947-C702-4D1A-B6EE-96B89D13953E} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{D38BB305-3276-4AE1-87B1-C19C7A9F3840} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{F7F4F27F-2CAE-422B-B6CE-25A88C3603A1} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{0E364862-1ACA-4C94-8EA0-23724FF67AA2} = {45B6292D-DC7E-46A7-A86E-7455BBEAED70}
Expand Down
7 changes: 7 additions & 0 deletions Rewrite/Rewrite.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.CSharp.Tests", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Test", "src\Rewrite.Test\Rewrite.Test.csproj", "{D3F48947-C702-4D1A-B6EE-96B89D13953D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Server", "src\Rewrite.Server\Rewrite.Server.csproj", "{D3F48947-C702-4D1A-B6EE-96B89D13953E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Test.CSharp", "src\Rewrite.Test.CSharp\Rewrite.Test.CSharp.csproj", "{3D70D4FA-3BA2-4230-B474-5A70DB23B8CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rewrite.Test.Engine.Remote", "src\Rewrite.Test.Engine.Remote\Rewrite.Test.Engine.Remote.csproj", "{6EC419CB-E60B-4F06-A710-B02EA6B9AAAC}"
Expand Down Expand Up @@ -85,6 +87,10 @@ Global
{D3F48947-C702-4D1A-B6EE-96B89D13953D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953D}.Release|Any CPU.Build.0 = Release|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3F48947-C702-4D1A-B6EE-96B89D13953E}.Release|Any CPU.Build.0 = Release|Any CPU
{3D70D4FA-3BA2-4230-B474-5A70DB23B8CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D70D4FA-3BA2-4230-B474-5A70DB23B8CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D70D4FA-3BA2-4230-B474-5A70DB23B8CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -109,6 +115,7 @@ Global
{5DD7D88A-5A10-457D-B1A2-30F7CF67AD9F} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{C7E280C5-E841-4690-8AA3-653ECBC7C78B} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{D3F48947-C702-4D1A-B6EE-96B89D13953D} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{D3F48947-C702-4D1A-B6EE-96B89D13953E} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{D38BB305-3276-4AE1-87B1-C19C7A9F3840} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{F7F4F27F-2CAE-422B-B6CE-25A88C3603A1} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
{8623AABC-D198-4D56-B26B-14A503B226E1} = {C7A4A8F9-0343-4A3E-AB77-101C458C23B5}
Expand Down
32 changes: 32 additions & 0 deletions Rewrite/src/Rewrite.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.CodeAnalysis;
using Parser = CommandLine.Parser;

namespace Rewrite.Server;

public class Program
{
public static void Main(string[] args)
{
var result = new Parser(ps =>
{
ps.AllowMultiInstance = true;
})
.ParseArguments<Options>(args);

if (result.Errors.Any())
{
foreach (var resultError in result.Errors)
{

Console.WriteLine(resultError);
}
throw new AggregateException(result.Errors.Select(e => new ArgumentException(e.ToString())));
}

var options = result.Value;

var server = new Server(options);

server.Listen().Wait();
}
}
38 changes: 38 additions & 0 deletions Rewrite/src/Rewrite.Server/Rewrite.Server.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PackageIcon>moderne.png</PackageIcon>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>


<ItemGroup Condition="'$(RELEASE_BUILD)'==''">
<ProjectReference Include="..\Rewrite.Core\Rewrite.Core.csproj" />
<ProjectReference Include="..\Rewrite.CSharp\Rewrite.CSharp.csproj" />
<ProjectReference Include="..\Rewrite.Java\Rewrite.Java.csproj" />
<ProjectReference Include="..\Rewrite.Json\Rewrite.Json.csproj" />
<ProjectReference Include="..\Rewrite.Properties\Rewrite.Properties.csproj" />
<ProjectReference Include="..\Rewrite.Xml\Rewrite.Xml.csproj" />
<ProjectReference Include="..\Rewrite.Yaml\Rewrite.Yaml.csproj" />
<ProjectReference Include="..\Rewrite.Remote.Codec\Rewrite.Remote.Codec.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(RELEASE_BUILD)'==''">
<ProjectReference Include="$(RewriteRemoteDir)\src\Rewrite.Remote\Rewrite.Remote.csproj" />
<ProjectReference Include="$(RewriteRemoteDir)\src\Rewrite.Remote.Server\Rewrite.Remote.Server.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(RELEASE_BUILD)'!=''">
<PackageReference Include="Rewrite.Remote" Version="$(RewriteRemoteVersion)"/>
<PackageReference Include="Rewrite.Remote.Server" Version="$(RewriteRemoteVersion)"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions rewrite-csharp-remote-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build/
target/
.gradle/
.idea/
out/
bin/
src/main/generated/
81 changes: 81 additions & 0 deletions rewrite-csharp-remote-server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
plugins {
id("org.openrewrite.build.language-library")
}

tasks.register<Zip>("zipDotnetServer") {
archiveFileName.set("DotnetServer.zip")
destinationDirectory.set(layout.buildDirectory.dir("tmp"))
from(rootProject.file("Rewrite/src/Rewrite.Server/bin/Release/net8.0/publish"))
include("*")
include("*/*") //to include contents of a folder present inside Reports directory
}

tasks.processResources {
dependsOn("zipDotnetServer")
from(layout.buildDirectory.file("tmp/DotnetServer.zip"))
}

tasks.compileJava {
options.release = 8
}

// We don't care about publishing javadocs anywhere, so don't waste time building them
tasks.withType<Javadoc>().configureEach {
enabled = false
}

tasks.named<Jar>("sourcesJar") {
enabled = false
}

tasks.named<Jar>("javadocJar") {
enabled = false
}

val emptySourceJar = tasks.create<Jar>("emptySourceJar") {
file("README.md")
archiveClassifier.set("sources")
}

val emptyJavadocJar = tasks.create<Jar>("emptyJavadocJar") {
file("README.md")
archiveClassifier.set("javadoc")
}

publishing {
publications.named<MavenPublication>("nebula") {
artifactId = project.name
description = project.description

artifacts.clear() // remove the regular JAR
// Empty JARs are OK: https://central.sonatype.org/publish/requirements/#supply-javadoc-and-sources
artifact(tasks.named("jar"))
artifact(emptySourceJar)
artifact(emptyJavadocJar)

pom {
name.set(project.name)
description.set(project.description)
url.set("https://moderne.io")
licenses {
license {
name.set("Moderne, Inc. Commercial License")
url.set("https://docs.moderne.io/administrator-documentation/references/licensing")
}
}
developers {
developer {
name.set("Team Moderne")
email.set("[email protected]")
organization.set("Moderne, Inc.")
organizationUrl.set("https://moderne.io")
}
}
scm {
connection.set("scm:git:git://github.com/moderneinc/rewrite-remote.git")
developerConnection.set("scm:git:ssh://github.com:moderneinc/rewrite-remote.git")
url.set("https://github.com/moderneinc/rewrite-remote/tree/main")
}
}
}
}
30 changes: 30 additions & 0 deletions rewrite-csharp-remote-server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2021 the original author or authors.
<p>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<p>
https://www.apache.org/licenses/LICENSE-2.0
<p>
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE"/>
</root>

</configuration>
6 changes: 6 additions & 0 deletions rewrite-csharp-remote-server/src/test/java/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

# Limit continuation indent to 2 spaces for Java files, as we heavily use continuations around our text blocks.
[*.java]
indent_size = 4
ij_continuation_indent_size = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openrewrite.remote.csharp;

import java.io.IOException;
import java.net.Socket;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.openrewrite.Cursor;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.remote.RemotingProjectParser;
import org.openrewrite.scheduling.WatchableExecutionContext;

public class ProjectParsingTest {

@Test
public void testParse() {
WatchableExecutionContext ctx =
new WatchableExecutionContext(new InMemoryExecutionContext());

int port = ThreadLocalRandom.current().nextInt(50000, 65535);
try (DotNetRemotingServerEngine server = DotNetRemotingServerEngine.create(DotNetRemotingServerEngine.Config.builder()
.extractedDotnetBinaryDir(Paths.get(System.getProperty("java.io.tmpdir")))
.port(port)
.logFilePath(Paths.get("./build/test.log").toAbsolutePath().toString())
.build())) {

server.start();
var client = new RemotingProjectParser(server);

Path pathToSolution = Path.of(Thread.currentThread()
.getContextClassLoader()
.getResource(
"ModerneHelloWorld/ModerneHelloWorld.sln")
.getPath());
client.findAllProjects(pathToSolution, ctx)
.forEach(proj -> client.parseProjectSources(proj,
pathToSolution,
pathToSolution.getParent(),
ctx)
.forEach(sf -> {
System.out.println(sf.print(new Cursor(new Cursor(
null,
Cursor.ROOT_VALUE), sf)));
}));
}
}

@Test
public void testThrowExceptionOnIncorrectDotnetPath() {
int port = ThreadLocalRandom.current().nextInt(50000, 65535);
try (DotNetRemotingServerEngine server = DotNetRemotingServerEngine.create(DotNetRemotingServerEngine.Config.builder().dotnetExecutable("dotnet1").extractedDotnetBinaryDir(
Paths.get(System.getProperty("java.io.tmpdir"))).port(port).build())) {
assertThatThrownBy(server::start)
.isInstanceOf(IOException.class);
}
}

@Test
public void testThrowExceptionOnIncorrectRunnable() {
int port = ThreadLocalRandom.current().nextInt(50000, 65535);
try (DotNetRemotingServerEngine server = DotNetRemotingServerEngine.create(DotNetRemotingServerEngine.Config.builder()
.extractedDotnetBinaryDir(Paths.get(System.getProperty("java.io.tmpdir")))
.dotnetServerDllName("Rewrite.Server1.dll")
.port(port)
.build())) {
assertThatThrownBy(server::start)
.isInstanceOf(IllegalStateException.class);
}
}
}
Loading

0 comments on commit f46aafc

Please sign in to comment.