|
| 1 | +#!/usr/bin/env ruby |
| 2 | +#/ Usage: ./action/update-photos |
| 3 | +#/ Update the local README photo wall to reflect the current developers listed in |
| 4 | +# bdougie/awesome-black-developers. |
| 5 | +require "yaml" |
| 6 | + |
| 7 | +class Readme |
| 8 | + DEFAULT_README = "README.md" |
| 9 | + |
| 10 | + def initialize(filename: DEFAULT_README) |
| 11 | + @filename = filename |
| 12 | + @developers = read_developer_yaml.merge(read_temp_file) |
| 13 | + end |
| 14 | + |
| 15 | + def read_developer_yaml |
| 16 | + file = File.open('developers.yaml') |
| 17 | + file_data = file.read |
| 18 | + file.close |
| 19 | + YAML::load(file_data) |
| 20 | + end |
| 21 | + |
| 22 | + def read_temp_file |
| 23 | + file = File.open('temp.txt') |
| 24 | + handle = "" |
| 25 | + links = [] |
| 26 | + file_data = file.readlines.each_with_index.map do |line, index| |
| 27 | + if index == 0 |
| 28 | + handle =line.gsub("#", "").strip |
| 29 | + else |
| 30 | + if !line.nil? && !line.strip.empty? |
| 31 | + links.push(line.gsub("*", "").strip) |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + {handle => links} |
| 36 | + end |
| 37 | + |
| 38 | + def update_developer_yaml |
| 39 | + yaml = @developers.to_yaml |
| 40 | + File.write('developers.yaml', yaml) |
| 41 | + end |
| 42 | + |
| 43 | + def preview |
| 44 | + [ |
| 45 | + "# Awesome Black Developers [](https://awesome.re)", |
| 46 | + "> Talks, blog posts, and interviews amplifying the voices of Black developers on GitHub because #BlackLivesMatter", |
| 47 | + build_photo_grid(@developers), |
| 48 | + build_developer_list(@developers), |
| 49 | + "## 💅🏾 Contributing", |
| 50 | + "Additional suggestions are welcomed! Check out [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.", |
| 51 | + "(NOTE: If you're a developer listed on here who would like to be removed, just open an issue or message me privately.)", |
| 52 | + "## 📖 License and attribution |
| 53 | + This list is available under the Creative Commons CC0 1.0 License, meaning you are free to use it for any purpose, commercial or non-commercial, without any attribution back to me (public domain). (If you ever want to reference me, find me here! [@bdougieYO](http://twitter.com/bdougieYO) But you are in no way required to do so.)" |
| 54 | + ].join("\n\n") |
| 55 | + end |
| 56 | + |
| 57 | + def save! |
| 58 | + update_developer_yaml |
| 59 | + File.write(@filename, preview) |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +def build_photo_grid(users) |
| 64 | + lines = [] |
| 65 | + |
| 66 | + users.map{|k, v| k}.each_slice(8) do |slice| |
| 67 | + header = slice.map { |e| handle_link(e) }.join(" | ").strip |
| 68 | + delimiter = slice.map { |e| "---" }.join(" | ") |
| 69 | + row = slice.map { |e| photo_link(e) }.join(" | ").strip |
| 70 | + |
| 71 | + lines += [header, delimiter, row, ""] |
| 72 | + end |
| 73 | + |
| 74 | + lines.join("\n") |
| 75 | +end |
| 76 | + |
| 77 | +def build_developer_list(users) |
| 78 | + row = users.map do |handle, links| |
| 79 | + developer_row = [] |
| 80 | + developer_row.push("### [@#{handle}](https://github.com/#{handle})") |
| 81 | + links.each do |link| |
| 82 | + developer_row.push(" * #{link}") |
| 83 | + end |
| 84 | + developer_row.join("\n") |
| 85 | + end |
| 86 | + row.join("\n\n") |
| 87 | +end |
| 88 | + |
| 89 | +def handle_link(login) |
| 90 | + "[@#{login}](https://github.com/#{login})" |
| 91 | +end |
| 92 | + |
| 93 | +def photo_link(login) |
| 94 | + "" |
| 95 | +end |
| 96 | + |
| 97 | +def update_readme(save: false) |
| 98 | + readme = Readme.new |
| 99 | + save ? readme.save! : (puts readme.preview) |
| 100 | +end |
| 101 | + |
| 102 | +update_readme(save: true) |
| 103 | + |
0 commit comments