有什么方法可以读取文件并获取Elixir中该文件中定义的模块列表,而无需使用ParallelCompiler方法?

时间:2019-07-18 21:49:29

标签: elixir

Kernel.ParallelCompiler.compile/2Kernel.ParallelCompiler.require/2传递文件列表时,例如if ...,返回Optional::orElseThrow

假设您已经编译了包含模块的文件,并且之前调用了Optional::ifPresent

是否可以在不调用def self.mass_increment(users, column_name = "events_count", count = 1) user_ids_for_sql = users.pluck(:id).join(',') # Example Result: user_ids_for_sql = "219,1,2" sanitized_sql = UserUnseenCount.send(:sanitize_sql_array, [ 'UPDATE user_unseen_counts SET ? = ? + ? WHERE user_id IN (?)', column_name, column_name, count, user_ids_for_sql ]) # Example Result: "UPDATE user_unseen_counts SET 'events_count' = 'events_count' + 1 WHERE user_id IN ('219,1,2')" sanitized_sql = sanitized_sql.gsub(/\'/, '') # Example Result: "UPDATE user_unseen_counts SET events_count = events_count + 1 WHERE user_id IN (219,1,2)" UserUnseenCount.connection.execute(sanitized_sql) end mod_fcgid: stderr: PHP Notice: Undefined variable: conn in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 219 mod_fcgid: stderr: PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 219 mod_fcgid: stderr: PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 229 mod_fcgid: stderr: PHP Notice: Trying to get property 'fare' of non-object in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 232 mod_fcgid: stderr: PHP Warning: Creating default object from empty value in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 234 mod_fcgid: stderr: PHP Notice: Undefined property: stdClass::$miles in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 239 mod_fcgid: stderr: PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 191 mod_fcgid: stderr: PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /var/www/vhosts/mytaxe.uk/public_html/fareadmin/include/app.php on line 193 的情况下获取Elixir中特定文件中包含的模块的列表?

1 个答案:

答案 0 :(得分:0)

Regex.scan/3

file_path = "./lots_of_modules.ex"
{:ok, contents} = File.read(file_path)
pattern = ~r{defmodule \s+ ([^\s]+) }x
Regex.scan(pattern, contents, capture: :all_but_first)
|> List.flatten()

输出:

["MyApp.UserManager.User", "Stuff", "Dog.Owner", "Pet.Cat.Meow"]

如果您需要模块名称作为原子:

  def get_modules() do
    file_path = "lib/lots_of_modules.ex"

    app_dir = File.cwd!
    absolute_file_path = Path.join([app_dir, file_path])

    {:ok, contents} = File.read(absolute_file_path)

    pattern = ~r{defmodule \s+ (\S+) }x  # \S is equivalent to [^\s]

    Regex.scan(pattern, contents, capture: :all_but_first)
    |> Enum.map(fn [name] -> 
                  String.to_existing_atom("Elixir.#{name}") 
                end)
  end

输出:

[MyApp.UserManager.User, Stuff, Dog.Owner, Pet.Cat.Meow]