I've been writing music since I was a teenager. But I never learned music theory until I discovered music theory YouTube tutorials.
I learned the rules that make up fundamental music structure. For example, a key must have 7 notes. It starts at the root note and goes up exactly 1-2 semitones with each next note in the scale.
Knowing that, you can programmatically build all possible keys and their scales.
But the best resource is the Key Finder. Whenever I'm playing with a tune and I have a few chords or notes figured out, I can put them in the Key Finder and get an idea of what other notes and chords would go along.