Last active
January 29, 2016 07:55
-
-
Save rsgrafx/3d32e5035a582b6e9f60 to your computer and use it in GitHub Desktop.
elixir rewrite folder structure from filenames.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Specifically written for - this problem with folders unzipped from this https://www.prepbootstrap.com website. | |
# may be able to help someone solve similar issue. | |
# | |
# Problem: Folder contains no folder structure - but hints at them in the file names: | |
# / | |
# Theme-LightWayAdmin\blog.html | |
# Theme-LightWayAdmin\font-awesome\less\list.less | |
# Theme-LightWayAdmin\bootstrap-elements.html | |
# Theme-LightWayAdmin\font-awesome\less\mixins.less | |
# Theme-LightWayAdmin\bootstrap-grid.html | |
# Theme-LightWayAdmin\font-awesome\less\path.less | |
# Theme-LightWayAdmin\bootstrap\css\bootstrap.css | |
# Theme-LightWayAdmin\bootstrap\fonts\glyphicons-halflings-regular.woff2 | |
# ... | |
# .exs file is ../ to target folder | |
# Does directory ls - reads in each file / regexs and builds folder structure | |
# Then copy's original file into that structure. | |
defmodule Elixirfolders do | |
# List all files in this folder | |
def start(url) do | |
url | |
|> folder | |
|> File.ls() | |
|> run(url) | |
end | |
defp folder(url) do | |
url | |
|> Path.absname | |
end | |
defp run {:ok, files}, url do | |
File.cd(url) | |
files | |
|> Enum.drop_while(fn(x) -> | |
x == ".DS_Store" | |
end) | |
|> Enum.drop_while(fn(x) -> | |
File.dir?( Path.absname(x) ) | |
end) | |
|> capture | |
end | |
defp run(_), do: IO.puts "No files found" | |
defp capture([]), do: IO.inspect("no more files") | |
defp capture([h | tail]) do | |
items = Regex.split(~r/\\/, h) | |
make_folders(items, h) | |
capture(tail) | |
end | |
defp make_folders(items, oldname) do | |
{ url, filename } = folder_struct(items) | |
_url = url |> folder | |
_file = oldname | |
|> Path.absname | |
# |> IO.inspect | |
if File.dir?(_url) do | |
File.cp!(oldname, "#{_url}/#{filename}") | |
|> IO.inspect | |
end | |
end | |
defp folder_struct(list) do | |
filename = List.last(list) | |
count = Enum.count(list) | |
url = list | |
|> List.delete_at((count-1)) | |
|> Mkdir.folders | |
{url, filename} | |
end | |
end | |
defmodule Mkdir do | |
def folders(list) do | |
Enum.reduce(list, [], fn(x, acc) -> | |
z = acc ++ [x] | |
File.mkdir_p(Enum.join(z, "/")) | |
acc ++ [x] | |
end) | |
|> Enum.join("/") | |
end | |
end | |
Elixirfolders.start("./Theme-LightWayAdmin") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment