Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use Expert.IEx.Helpers

try do
Mix.ensure_application!(:observer)
rescue
_ -> nil
end
2 changes: 2 additions & 0 deletions .iex.namespaced.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use XPExpert.Server.IEx.Helpers
alias XPExpert, as: EXpert
6 changes: 2 additions & 4 deletions apps/engine/lib/engine/plugin/discovery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Engine.Plugin.Discovery do

@namespaced_document_module [:Forge, :Document]
|> Module.concat()
|> Forge.Namespace.Module.run(apps: [:forge], roots: [Forge])
|> Forge.Namespace.Module.apply()

def run do
for {app_name, _, _} <- :application.loaded_applications(),
Expand Down Expand Up @@ -49,12 +49,10 @@ defmodule Engine.Plugin.Discovery do
end

defp namespace_module(module) when is_atom(module) do
app = Application.get_application(module)

module
|> :code.which()
|> List.to_string()
|> Forge.Namespace.Transform.Beams.apply_to_all(apps: [app])
|> Forge.Namespace.Transform.Beams.apply()
end

defp unload_module(module) do
Expand Down
14 changes: 14 additions & 0 deletions apps/expert/.iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
alias Forge.Project

other_project =
[
File.cwd!(),
"..",
"..",
"..",
"eakins"
]
|> Path.join()
|> Path.expand()

project = Forge.Project.new("file://#{other_project}")
20 changes: 1 addition & 19 deletions apps/expert/lib/expert/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,13 @@ defmodule Expert.Application do

@impl true
def start(_type, _args) do
{m, f, a} = Application.get_env(:expert, :arg_parser)

argv = apply(m, f, a)

{opts, _, _invalid} =
OptionParser.parse(argv,
strict: [port: :integer]
)

buffer_opts =
case opts[:port] do
port when is_integer(port) ->
[communication: {GenLSP.Communication.TCP, [port: port]}]

_ ->
[]
end

children = [
document_store_child_spec(),
{DynamicSupervisor, Expert.Project.DynamicSupervisor.options()},
{DynamicSupervisor, name: Expert.DynamicSupervisor},
{GenLSP.Assigns, [name: Expert.Assigns]},
{Task.Supervisor, name: :expert_task_queue},
{GenLSP.Buffer, [name: Expert.Buffer] ++ buffer_opts},
{GenLSP.Buffer, name: Expert.Buffer},
{Expert,
name: Expert,
buffer: Expert.Buffer,
Expand Down
12 changes: 2 additions & 10 deletions apps/expert/lib/expert/engine_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,11 @@ defmodule Expert.EngineNode do
# When Engine is built in CI for a version matrix, we'll need to check if
# we have the right version downloaded, and if not, we should download it.
defp glob_paths do
engine_path = System.get_env("EXPERT_ENGINE_PATH", default_engine_path())

Logger.info("Using Engine path: #{Path.expand(engine_path)}")

engine_path
|> Path.expand()
:expert
|> :code.priv_dir()
|> Path.join("lib/**/ebin")
|> Path.wildcard()
end

defp default_engine_path do
:expert |> :code.priv_dir() |> Path.join("engine")
end
end

@stop_timeout 1_000
Expand Down
9 changes: 4 additions & 5 deletions apps/expert/lib/expert/release.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Expert.Release do
def assemble(release) do
Mix.Task.run(:namespace, [release.path])

engine_path = Path.expand("../../../engine", __DIR__)

source = Path.join([engine_path, "_build/dev_ns"])
Expand All @@ -8,13 +10,10 @@ defmodule Expert.Release do
Path.join([
release.path,
"lib",
"#{release.name}-#{release.version}",
"priv",
"engine"
"xp_expert-#{release.version}",
"priv"
])

File.mkdir_p!(dest)

File.cp_r!(source, dest)

release
Expand Down
2 changes: 1 addition & 1 deletion apps/expert/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Expert.MixProject do

def application do
[
extra_applications: [:logger, :runtime_tools, :kernel, :erts, :observer, :wx],
extra_applications: [:logger, :runtime_tools, :kernel, :erts, :observer],
mod: {Expert.Application, []}
]
end
Expand Down
22 changes: 1 addition & 21 deletions apps/forge/lib/forge/namespace/abstract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ defmodule Forge.Namespace.Abstract do
https://www.erlang.org/doc/apps/erts/absform.html
"""

def code_from(path) do
with {:ok, {_orig_module, code_parts}} <- :beam_lib.chunks(path, [:abstract_code]),
{:ok, {:raw_abstract_v1, forms}} <- Keyword.fetch(code_parts, :abstract_code) do
{:ok, forms}
else
_ ->
{:error, :not_found}
end
end

def run(abstract_format, opts) when is_list(abstract_format) do
fn ->
Process.put(:abstract_code_opts, opts)
Enum.map(abstract_format, fn af -> rewrite(af) end)
end
|> Task.async()
|> Task.await()
end

def rewrite(abstract_format) when is_list(abstract_format) do
Enum.map(abstract_format, &rewrite/1)
end
Expand Down Expand Up @@ -313,7 +294,6 @@ defmodule Forge.Namespace.Abstract do
end

defp rewrite_module(module) do
opts = Process.get(:abstract_code_opts)
Forge.Namespace.Module.run(module, opts)
Forge.Namespace.Module.apply(module)
end
end
26 changes: 8 additions & 18 deletions apps/forge/lib/forge/namespace/module.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
defmodule Forge.Namespace.Module do
@namespace_prefix "XP"

def run(module_name, opts) do
apps = Keyword.fetch!(opts, :apps)
roots = Keyword.fetch!(opts, :roots)

def apply(module_name) do
cond do
prefixed?(module_name) ->
module_name

opts[:do_apps] && module_name in apps ->
module_name in Mix.Tasks.Namespace.app_names() ->
:"xp_#{module_name}"

true ->
module_name
|> Atom.to_string()
|> apply_namespace(roots)
|> apply_namespace()
end
end

Expand All @@ -31,7 +28,7 @@ defmodule Forge.Namespace.Module do
def prefixed?(@namespace_prefix <> _),
do: true

def prefixed?("xp_" <> _),
def prefixed?("xp" <> _),
do: true

def prefixed?([?x, ?p, ?_ | _]), do: true
Expand All @@ -41,9 +38,8 @@ defmodule Forge.Namespace.Module do
def prefixed?(_),
do: false

defp apply_namespace("Elixir." <> rest, roots) do
roots
|> Enum.filter(fn module -> Macro.classify_atom(module) == :alias end)
defp apply_namespace("Elixir." <> rest) do
Mix.Tasks.Namespace.root_modules()
|> Enum.map(fn module -> module |> Module.split() |> List.first() end)
|> Enum.reduce_while(rest, fn root_module, module ->
if has_root_module?(root_module, module) do
Expand All @@ -61,14 +57,8 @@ defmodule Forge.Namespace.Module do
|> Module.concat()
end

defp apply_namespace(erlang_module, roots) do
erlang_module = String.to_atom(erlang_module)

if erlang_module in roots do
:"xp_#{erlang_module}"
else
erlang_module
end
defp apply_namespace(erlang_module) do
String.to_atom(erlang_module)
end

defp has_root_module?(root_module, root_module), do: true
Expand Down
40 changes: 19 additions & 21 deletions apps/forge/lib/forge/namespace/path.ex
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
defmodule Forge.Namespace.Path do
alias Forge.Namespace

def run(path, opts) when is_list(path) do
def apply(path) when is_list(path) do
path
|> List.to_string()
|> run(opts)
|> apply()
|> String.to_charlist()
end

def run(path, opts) when is_binary(path) do
apps = Keyword.fetch!(opts, :apps)

def apply(path) when is_binary(path) do
path
|> Path.split()
|> Enum.map(fn path_component ->
Enum.reduce(apps, path_component, fn app_name, path ->
[path | vsn] = String.split(path, "-")
|> Enum.map(&replace_namespaced_apps/1)
|> Path.join()
end

defp replace_namespaced_apps(path_component) do
Enum.reduce(Mix.Tasks.Namespace.app_names(), path_component, fn app_name, path ->
[path | vsn] = String.split(path, "-")

if path == Atom.to_string(app_name) do
new_path =
app_name
|> Namespace.Module.run(opts)
|> Atom.to_string()
if path == Atom.to_string(app_name) do
new_path =
app_name
|> Forge.Namespace.Module.apply()
|> Atom.to_string()

rebuild_path(new_path, vsn)
else
rebuild_path(path, vsn)
end
end)
rebuild_path(new_path, vsn)
else
rebuild_path(path, vsn)
end
end)
|> Path.join()
end

defp rebuild_path(path, []), do: path
Expand Down
36 changes: 24 additions & 12 deletions apps/forge/lib/forge/namespace/transform/app_directories.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
defmodule Forge.Namespace.Transform.AppDirectories do
alias Forge.Namespace

def apply_to_all(base_directory, opts) do
app_globs = Enum.join(opts[:apps], "*,")

def apply_to_all(base_directory) do
base_directory
|> Path.join("lib/{#{app_globs}*}")
|> Path.wildcard()
|> Enum.each(fn d -> run(d, opts) end)
|> find_app_directories()
|> Enum.each(&apply_transform(base_directory, &1))
end

def run(app_path, opts) do
namespaced_app_path = Namespace.Path.run(app_path, opts)
def apply_transform(base_directory, app_path) do
namespaced_relative_path =
app_path
|> Path.relative_to(base_directory)
|> Forge.Namespace.Path.apply()

namespaced_app_path = Path.join(base_directory, namespaced_relative_path)

with true <- app_path != namespaced_app_path,
{:ok, _} <- File.rm_rf(namespaced_app_path) do
with {:ok, _} <- File.rm_rf(namespaced_app_path) do
File.rename!(app_path, namespaced_app_path)
end
catch
e ->
Mix.Shell.IO.error("Failed to rename app directory")
reraise e, __STACKTRACE__
end

defp find_app_directories(base_directory) do
app_names = Mix.Tasks.Namespace.app_names()
app_globs = Enum.join(app_names, "*,")

[base_directory, "lib", "{" <> app_globs <> "*}"]
|> Path.join()
|> Path.wildcard()
end
end
Loading
Loading