Skip to content

Instantly share code, notes, and snippets.

@jakeprem
Created September 3, 2024 23:00
Show Gist options
  • Select an option

  • Save jakeprem/8a301bd64cd27bd656b8bd84d12aea17 to your computer and use it in GitHub Desktop.

Select an option

Save jakeprem/8a301bd64cd27bd656b8bd84d12aea17 to your computer and use it in GitHub Desktop.

Revisions

  1. jakeprem created this gist Sep 3, 2024.
    45 changes: 45 additions & 0 deletions concat.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    defmodule Mix.Tasks.Concat do
    @shortdoc "Concatenates all Elixir code in the project to a single file"
    @moduledoc false
    use Mix.Task

    def run(args) do
    {opts, _, _} =
    OptionParser.parse(args,
    switches: [output: :string],
    aliases: [o: :output]
    )

    source_dir = File.cwd!()
    output_file = opts[:output] || "concatenated_code"

    concatenate_project(source_dir, output_file)

    Mix.shell().info("Code concatenated to #{output_file}")
    end

    defp concatenate_project(source_dir, output_file) do
    paths = %{
    "podke" => "lib/podke",
    "podke_web" => "lib/podke_web"
    }

    for {name, path} <- paths do
    output_filename = "#{name}_#{output_file}"
    # File.write!(output_filename, "", [:write])

    source_dir
    |> Path.join(path)
    |> Path.join("**/*.{ex,exs}")
    |> Path.wildcard()
    |> Enum.sort()
    |> Enum.each(fn file ->
    content = File.read!(file)

    File.write!("#{output_filename}.txt", "# File: #{file}\n\n#{content}\n\n", [
    :append
    ])
    end)
    end
    end
    end