-
-
Save bf4/5d978227543ce04236ef2ae6bacc5393 to your computer and use it in GitHub Desktop.
Feature flag code detection
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
# https://gist.github.com/synth/8c9eee23aa9df535aa42a30f7cff9ba9 | |
require "parser/current" | |
module Flipper | |
module FlagRegistration | |
# These functions are all memoized because they should be static for the | |
# lifetime of a deployment (albeit they are really static to a Ruby process) | |
def self.registered_flags | |
@registered_flags ||= YAML.load_file("config/feature_flags.yml") | |
end | |
def self.registered_flags=(flags) | |
File.write("config/feature_flags.yml", flags.to_yaml) | |
end | |
def self.flags_in_code | |
@flags_in_code ||= begin | |
search_regex = 'Flipper.enabled\?\([^\)]+\)' | |
command = "grep -rohE --exclude-dir='.//.*' --include='*rb' '#{search_regex}' ./" | |
results = `#{command}` | |
lines = results.split("\n") | |
lines.filter_map { |line| | |
ast = Parser::CurrentRuby.parse(line) | |
ast.children[2].children[0]&.to_s | |
}.sort.uniq | |
end | |
end | |
def self.registered_flags_not_found | |
@registered_flags_not_found ||= flags_in_code - registered_flags.keys.map(&:to_sym) | |
end | |
def self.flags_not_registered | |
@flags_not_registered ||= flags_in_code - registered_flags.keys.map(&:to_sym) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment